Exception handling is a mechanism in Python (and in many other programming languages) that allows you to gracefully handle and manage errors that might occur during the execution of your program. Instead of letting an error crash your program, you can use exception handling to catch the error, take appropriate actions, and continue the program's execution.
Here's a basic structure of how exception handling works in Python:
try:
# Code that might raise an exception
result = 10 / 0 # This will raise a ZeroDivisionError
except ExceptionType:
# Code to handle the exception
print("An exception occurred!")
else:
# Code to execute if no exception occurs
print("No exception occurred.")
finally:
# Code that always executes, whether an exception occurred or not
print("This will always be executed.")
Key components of exception handling:
try: The block of code where you suspect an exception might occur.
except: The block of code that is executed if an exception of the specified type occurs in the
try
block.else (optional): The block of code that is executed if no exception occurs in the
try
block.finally (optional): The block of code that is always executed, whether an exception occurred or not.
Here's a practical example:
try:
num = int(input("Enter a number: "))
result = 10 / num
except ZeroDivisionError:
print("You can't divide by zero!")
except ValueError:
print("Invalid input. Please enter a valid number.")
else:
print("Result:", result)
finally:
print("Exiting exception handling.")
In this example, if the user enters a non-numeric input or if they enter zero, the appropriate exception block will be executed, preventing the program from crashing. If no exception occurs, the else
block will execute, and the finally
block will always execute regardless of whether an exception occurred.
Python provides a wide range of built-in exceptions that you can catch and handle. It's also possible to create your own custom exceptions to suit your program's needs.
Exception handling is a powerful tool to make your programs more robust and user-friendly by gracefully handling unexpected situations.