Skip to main content

Error Handling and Exception Handling

Errors and exceptions are an inevitable part of programming. Python provides a robust error handling mechanism to catch and handle these errors gracefully. In this section, we'll explore how to handle errors and exceptions effectively in Python.

The try-except Block

The try-except block is used to handle exceptions in Python. The code inside the try block is executed, and if an exception occurs, it is caught and handled in the except block. Here's a basic example:

try:
# Code that may raise an exception
result = 10 / 0
except:
# Code to handle the exception
print("An error occurred!")

In the above code, we attempt to divide 10 by 0, which raises a ZeroDivisionError. The except block catches this exception and executes the code inside it, printing the error message.

Handling Specific Exceptions

You can handle specific exceptions by specifying the type of exception after the except keyword. This allows you to provide different handling logic for different types of exceptions. Here's an example:

try:
# Code that may raise an exception
result = 10 / 0
except ZeroDivisionError:
# Code to handle the ZeroDivisionError
print("Cannot divide by zero!")
except ValueError:
# Code to handle the ValueError
print("Invalid value!")

In the above code, we have separate except blocks for ZeroDivisionError and ValueError. Depending on the type of exception that occurs, the corresponding block will be executed.

The else Block

You can also include an else block after the except block to specify code that should be executed when no exceptions occur. This block is optional. Here's an example:

try:
# Code that may raise an exception
result = 10 / 2
except ZeroDivisionError:
# Code to handle the ZeroDivisionError
print("Cannot divide by zero!")
else:
# Code to execute if no exceptions occur
print("Result:", result)

In the above code, the else block will be executed if no exception occurs. In this case, it prints the result of the division.

The finally Block

The finally block is used to specify code that should be executed regardless of whether an exception occurred or not. This block is optional but is commonly used for cleanup operations. Here's an example:

try:
# Code that may raise an exception
result = 10 / 2
except ZeroDivisionError:
# Code to handle the ZeroDivisionError
print("Cannot divide by zero!")
finally:
# Code to execute regardless of exceptions
print("End of operation")

In the above code, the finally block will always be executed, whether an exception occurred or not. It prints "End of operation" to indicate the end of the try-except block.

Raising Exceptions

You can also manually raise exceptions using the raise statement. This allows you to create custom exceptions or raise built-in exceptions based on certain conditions. Here's an example:

try:
age = int(input("Enter your age: "))
if age < 0:
raise ValueError("Age cannot be negative!")
except ValueError as e:
print(str(e))
else:
print(f"Entered age {age}")

In the above code, we ask the user to enter their age. If the entered age is negative, we raise a ValueError with a custom error message.

Summary

In this section, we explored error handling and exception handling in Python. We learned how to use the try-except block to catch and handle exceptions, handle specific exceptions, include the else and finally blocks, and raise exceptions. Proper error handling ensures that your code can gracefully handle unexpected situations and provides a better user experience.

Github Repo

info

You can refer to and clone the code samples for this tutorial from the GitHub repository.

To clone the repository, you can use the following command:

git clone https://github.com/certifysphere/python-code-samples.git

You can then navigate to the /src directory to access all the code samples given in this tutorial.