Exception handling is an essential part of writing robust Python programs. It allows your program to respond to runtime errors, ensuring that the program doesn't crash unexpectedly.
Errors: An error occurs in Python when something goes wrong during program execution. Errors can be of two types:
Exceptions: An exception is a special kind of runtime error that can be caught and handled using Python's exception handling mechanisms (try
, except
, finally
).
An exception is an event that disrupts the normal flow of a program's execution. When an error occurs, Python raises an exception that can be handled by the programmer to manage the situation.
Python provides the try
, except
, else
, and finally
blocks to handle exceptions.
try
Block: Code that might raise an exception is placed inside the try
block.except
Block: If an exception is raised in the try
block, the code in the except
block is executed.else
Block: If no exception is raised, the code in the else
block is executed.finally
Block: This block always runs, regardless of whether an exception is raised or not. It is usually used for cleanup actions.Basic Syntax:
try:
# Code that might raise an exception
except SomeException:
# Code that runs if an exception occurs
else:
# Code that runs if no exception occurs
finally:
# Code that always runs, regardless of exceptions
Example:
try:
x = 10 / 0 # Division by zero error
except ZeroDivisionError:
print("You cannot divide by zero!")
else:
print("Division was successful.")
finally:
print("This will always execute.")
Output:
You cannot divide by zero!
This will always execute.
You can handle multiple exceptions by using multiple except
blocks or a single except
block with multiple exception types.
try:
x = int(input("Enter a number: "))
result = 10 / x
except ValueError:
print("Invalid input! Please enter an integer.")
except ZeroDivisionError:
print("Cannot divide by zero!")
else:
print(f"The result is {result}")
finally:
print("Execution complete.")
Sometimes, you may want to catch all exceptions without specifying each one. You can use a generic except
block.
Example:
try:
# Some code that might raise any exception
x = int("hello")
except Exception as e:
print(f"An error occurred: {e}")
finally:
print("Execution complete.")
Output:
An error occurred: invalid literal for int() with base 10: 'hello'
Execution complete.
You can define custom exceptions by creating a new class that inherits from Python's built-in Exception
class.
class CustomError(Exception):
pass
try:
raise CustomError("This is a custom error")
except CustomError as ce:
print(f"Caught an error: {ce}")
Output:
Caught an error: This is a custom error
raise
to Trigger ExceptionsThe raise
statement is used to trigger exceptions manually. You can raise built-in exceptions or custom exceptions.
Example:
def check_age(age):
if age < 18:
raise ValueError("Age must be 18 or older")
else:
print("Age is valid")
try:
check_age(16)
except ValueError as e:
print(e)
Output:
Age must be 18 or older
Exception
to avoid masking other errors.except
blocks: Empty blocks can hide bugs, so ensure you handle exceptions properly.finally
for cleanup: Use the finally
block for operations like closing files or database connections, which should happen regardless of success or failure.Objective: Strengthen your exception handling skills by addressing various common scenarios.
Basic Exception Handling:
ZeroDivisionError
: When dividing by zero.ValueError
: When the user enters non-numeric values.try:
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
result = num1 / num2
except ZeroDivisionError:
print("Error: Cannot divide by zero.")
except ValueError:
print("Error: Please enter valid numbers.")
else:
print(f"The result is: {result}")
Handling Multiple Exceptions:
ValueError
. If the number is less than 10, raise a custom exception ValueTooSmallError
.class ValueTooSmallError(Exception):
pass
try:
num = float(input("Enter a number: "))
if num < 10:
raise ValueTooSmallError("The number is too small.")
except ValueError:
print("Error: Invalid input. Please enter a valid number.")
except ValueTooSmallError as e:
print(f"Error: {e}")
Custom Exceptions:
NegativeValueError
that is raised when the user inputs a negative number. Handle this exception appropriately.class NegativeValueError(Exception):
def __init__(self, message="Negative value not allowed"):
self.message = message
super().__init__(self.message)
try:
value = float(input("Enter a positive number: "))
if value < 0:
raise NegativeValueError()
except NegativeValueError as e:
print(f"Error: {e}")
Using the finally
Block:
try:
file = open("sample.txt", "r")
contents = file.read()
print(contents)
except FileNotFoundError:
print("Error: The file was not found.")
finally:
if 'file' in locals():
file.close()
print("File has been closed.")