DEV Community

Cover image for Handling Exceptions in Python
Paulo GP
Paulo GP

Posted on • Updated on

Handling Exceptions in Python

Introduction

In this chapter, we'll explore how to handle exceptions using Python. We'll provide code examples with the output to demonstrate how to use various techniques for managing exceptions, including try-catch blocks, raising exceptions, and using the traceback module.

Using Try-Catch Blocks

One way to handle exceptions in Python is to use try-catch blocks. These blocks allow us to execute a code block and catch any exceptions that may occur during its execution. Here's an example that demonstrates how to use a try-catch block to handle a division by zero exception:

try:
    x = 5 / 0
except ZeroDivisionError:
    print("Error: Cannot divide by zero")
Enter fullscreen mode Exit fullscreen mode
Output:
Error: Cannot divide by zero
Enter fullscreen mode Exit fullscreen mode

In this example, we attempt to divide 5 by 0, which raises a ZeroDivisionError exception. This exception is caught in the except block, and we print an error message to inform the user that division by zero is not allowed.

Raising Exceptions

In addition to catching exceptions, we can also raise our exceptions using the raise statement. This allows us to create custom exceptions that can be used to handle specific error conditions. Here's an example that demonstrates how to raise an exception when a negative number is passed to a square root function:

import math

def sqrt(x):
    if x < 0:
        raise ValueError("Cannot take the square root of a negative number")
    return math.sqrt(x)

try:
    result = sqrt(-1)
except ValueError as e:
    print(e)
Enter fullscreen mode Exit fullscreen mode
Output:
Cannot take the square root of a negative number
Enter fullscreen mode Exit fullscreen mode

In this example, we define a sqrt function that takes the square root of a number. If the number is negative, we raise a ValueError exception with a custom error message. This exception is caught in the try block, and we print the error message to inform the user that the square root of a negative number is not allowed.

Using the Traceback Module

When an exception occurs, Python provides a traceback that shows the call stack at the point where the exception was raised. This can be useful for debugging, as it allows us to see where the error occurred and what led to it. We can use the traceback module to access and format this information. Here's an example that demonstrates how to use the traceback module to print a traceback when an exception occurs:

import traceback

try:
    x = 5 / 0
except ZeroDivisionError:
    print("Error: Cannot divide by zero")
    print(traceback.format_exc())
Enter fullscreen mode Exit fullscreen mode
Output:
Error: Cannot divide by zero
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
ZeroDivisionError: division by zero
Enter fullscreen mode Exit fullscreen mode

In this example, we attempt to divide 5 by 0, which raises a ZeroDivisionError exception. This exception is caught in the except block, and we print an error message to inform the user that division by zero is not allowed. We also use the traceback.format_exc function to print a formatted traceback that shows the call stack at the point where the exception was raised.

Using the Else Clause in Try-Catch Blocks

In addition to the try and except clauses, we can also use an else clause in a try-catch block. The code in the else clause is executed if no exceptions are raised in the try block. Here's an example that demonstrates how to use the else clause in a try-catch block:

try:
    x = 5 / 2
except ZeroDivisionError:
    print("Error: Cannot divide by zero")
else:
    print("Result:", x)
Enter fullscreen mode Exit fullscreen mode
Output:
Result: 2.5
Enter fullscreen mode Exit fullscreen mode

In this example, we attempt to divide 5 by 2, which does not raise any exceptions. Since no exceptions are raised, the code in the else clause is executed, and we print the result of the division.

Adding Notes

We can also add notes to our code to provide additional information or context about the code. Here's an example that demonstrates how to use the add_note function to add a note to our code:

def add_note():
    print("Note: This function calculates the square root of a number")

def sqrt(x):
    if x < 0:
        raise ValueError("Cannot take the square root of a negative number")
    return math.sqrt(x)

add_note()
try:
    result = sqrt(-1)
except ValueError as e:
    print(e)
Enter fullscreen mode Exit fullscreen mode
Output:
Note: This function calculates the square root of a number
Cannot take the square root of a negative number
Enter fullscreen mode Exit fullscreen mode

In this example, we define an add_note function that prints a note about the sqrt function. We then call this function before calling the sqrt function to add a note to our code. The note is printed when we run the code, followed by the error message raised by the sqrt function.

Conclusion

In summary, Python provides several tools and techniques for handling exceptions. By using try-catch blocks, raising exceptions, and using the traceback module, we can manage exceptions flexibly and robustly. Additionally, we can use notes to provide additional information or context about our code.

Top comments (0)