Handling Exceptions
In this tutorial, we will explore how to handle exceptions in Python using the try
and except
blocks. We'll start by understanding the basic syntax and then delve into practical examples to demonstrate their usage.
Syntax of try-except block
The try
block is used to enclose the code that might raise an exception. It's followed by one or more except
blocks, which provide an opportunity to handle specific types of exceptions.
try:
# Risky code that may raise an exception
risky_code()
except ExceptionType1:
# Handle ExceptionType1
handle_exception1()
except ExceptionType2:
# Handle ExceptionType2
handle_exception2()
# Add more except blocks as needed
Basic Example: Handling Division by Zero
Let's start with a basic example where we attempt to divide by zero:
try:
result = 10 / 0 # Division by zero
except ZeroDivisionError as e:
print(f"Error: {e}")
In this example, the try
block attempts the division operation, and since dividing by zero raises a ZeroDivisionError
, it's caught in the except
block where we print the error message.
Being Specific with Exceptions
It's generally considered good practice to be specific about the exceptions you handle. Let's consider another example:
while True:
try:
user_input = input("Enter a number: ")
result = 10 / float(user_input)
break # Exit loop if successful
except ZeroDivisionError:
print("Error: You cannot divide by zero.")
except ValueError:
print("Error: Please enter a valid number.")
In this example, we attempt to convert user input to a float and perform a division. We handle ZeroDivisionError
and ValueError
separately, providing appropriate error messages for each scenario.
Handling Generic Exceptions
While it's preferable to be specific, you can also catch generic exceptions using except Exception
. However, it's usually a last resort as it catches all exceptions, making debugging more challenging.
try:
# Risky operation
risky_operation()
except Exception as e:
print(f"An error occurred: {e}")
Incorporating Exception Handling in a Program
Now, let's integrate exception handling into a simple calculator program:
import sys
total = 0.0
while True:
try:
user_input = input("Enter a number: ")
if user_input == '0':
print(f"Total: {total}")
sys.exit() # Exit the program
total += float(user_input)
except ValueError:
print("Error: Please enter a valid number.")
In this program, we continuously prompt the user for input and update the total. If the user enters '0', we print the total and exit the program. We handle ValueError
if the user input cannot be converted to a float.
Conclusion
Exception handling in Python allows us to gracefully manage errors and unexpected behavior in our programs. By using try
and except
blocks, we can anticipate potential issues and provide appropriate responses, improving the robustness and reliability of our code. Remember to be specific with exception handling and use generic exception handling sparingly.