Welcome to codeswithpankaj.com! In this tutorial, we will explore the concept of exception handling in Python. We'll cover what exceptions are, how to handle them, and provide detailed examples to illustrate its application.
- Introduction to Exceptions
- Why Use Exception Handling?
- Types of Exceptions
- The
tryandexceptBlocks - Handling Multiple Exceptions
- The
elseandfinallyBlocks - Raising Exceptions
- Custom Exceptions
- Practical Examples
- Summary
Exceptions are events that occur during the execution of a program that disrupt the normal flow of instructions. They are typically errors that need to be addressed to prevent the program from crashing.
- Exceptions indicate errors and exceptional conditions in the program.
- Python provides a robust mechanism to handle exceptions gracefully.
- Prevent Crashes: Exception handling prevents the program from terminating unexpectedly.
- Debugging: It helps in debugging by providing detailed error messages.
- Graceful Degradation: It allows the program to continue execution or fail gracefully.
Python has many built-in exceptions, such as:
SyntaxErrorTypeErrorIndexErrorKeyErrorValueErrorZeroDivisionError
The try block allows you to test a block of code for errors. The except block lets you handle the error.
try:
x = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero")You can handle multiple exceptions by specifying multiple except blocks.
try:
x = int("abc")
except ValueError:
print("ValueError occurred")
except TypeError:
print("TypeError occurred")try:
x = int("abc")
except (ValueError, TypeError) as e:
print(f"An error occurred: {e}")The else block is executed if no exceptions are raised in the try block.
try:
x = int("10")
except ValueError:
print("ValueError occurred")
else:
print("No error occurred")The finally block is executed regardless of whether an exception occurs or not.
try:
x = int("10")
except ValueError:
print("ValueError occurred")
finally:
print("This will always be executed")You can raise an exception using the raise statement.
def check_age(age):
if age < 18:
raise ValueError("Age must be at least 18")
return "Age is valid"
try:
print(check_age(16))
except ValueError as e:
print(e)You can create custom exceptions by defining a new class that inherits from the built-in Exception class.
class CustomError(Exception):
pass
def check_value(value):
if value < 0:
raise CustomError("Value must be non-negative")
try:
check_value(-1)
except CustomError as e:
print(e)try:
with open("nonexistent_file.txt", "r") as file:
content = file.read()
except FileNotFoundError:
print("File not found")
else:
print(content)
finally:
print("File operation complete")def divide(a, b):
try:
result = a / b
except ZeroDivisionError:
return "Cannot divide by zero"
except TypeError:
return "Invalid input type"
else:
return result
finally:
print("Division operation complete")
print(divide(10, 2))
print(divide(10, 0))
print(divide(10, "a"))class InvalidInputError(Exception):
def __init__(self, message):
self.message = message
super().__init__(self.message)
def get_positive_number():
try:
number = int(input("Enter a positive number: "))
if number <= 0:
raise InvalidInputError("The number must be positive")
return number
except ValueError:
return "Invalid input type"
except InvalidInputError as e:
return e.message
print(get_positive_number())In this tutorial, we explored the concept of exception handling in Python, its importance, and how to implement it using try, except, else, and finally blocks. We also covered raising exceptions, creating custom exceptions, and practical examples to illustrate the application of exception handling. Exception handling is a powerful feature that enhances code robustness, readability, and maintainability.
For more tutorials and in-depth explanations, visit codeswithpankaj.com!
This tutorial provides a comprehensive overview of Python exception handling, detailing each topic and subtopic with examples and explanations. For more such tutorials, keep following codeswithpankaj.com!