How to ignore an exception and proceed in Python?


An exception is an unexpected error or event that occurs during the execution of program. The difference between an error and an exception in a program is that, when an exception is encountered, the program deflects from its original course of execution whereas when an error occurs, the program is terminated. Hence, unlike errors, an exception can be handled. Therefore, you won't have a program crash.

However, in some cases of Python, the exception might not cause the program to terminate and will not affect the direction of execution hugely. Therefore, it is best to ignore such kind of exceptions. There are several ways to do this.

  • Using the try/except block

  • Using the suppress() method

Using the try/except Block

Try/except blocks will allow you to execute code and skip any errors that occur using the "pass" statement. This will essentially do nothing, but it will ignore any errors that occur. The syntax for this can be seen below −

try:
   code to be executed that may cause an exception
except:
   pass

You can also ignore specific exceptions by using the except statement followed by a specific exception. For example, to ignore the AttributeError exception, you can use the following code

try:
   # lines of code
except AttributeError:
   pass

If you want to ignore all exceptions, you can use the following code −

try:
   # lines of code
except Exception:
   pass

Example

In the following example, let us perform the division operation on an integer iteratively. Using for loop, we divide the integer object in a range 0-5; but zero division is not possible and the program will raise a ZeroDivisionError. This exception is ignored using the “pass” statement and execute the next iterations.

a = 16
for i in range(5):
    try:
        print(a/i)
    except:
        pass

Output

The output for the program above is given below. The program ignores the first iteration as the first division is zero division.

16.0
8.0
5.333333333333333
4.0

Using suppress() Method

Another optimal way to ignore the exceptions, instead of using the try/except blocks, is the suppress() method. This method belongs to the contextlib module in Python.

However, unlike the try/except block, if an exception is raised inside this context, the entire lines of code within it will be suppressed by the python interpreter. Let us look at some examples to understand this in a better way.

Example

In this example, we are trying to suppress/ignore the zero division error using the suppress() method.

import contextlib
a = 16
with contextlib.suppress(ZeroDivisionError):
    div = a/0
print("Zero division error is suppressed")

Output

The output for the program above will be produced as follows −

Zero division error is suppressed

Example

Even if single line of code within the suppress() method context raises an exception, the other lines enclosed in this block will also not be executed. This is the only drawback of this method. Let us see an example demonstrating the same below.

import contextlib
a = 16
with contextlib.suppress(Exception):
    for i in range(5):
        print(a/i)
print("The entire loop is not displayed")

Output

On executing the program above, the output only displays the statement outside the context even though only one iteration of the loop raises an exception −

The entire loop is not displayed

Conclusion

In Python, you can ignore an exception and continue processing by using the "try...except" construct. If an exception occurs, the "except" block will be executed, and you can handle the exception accordingly. If you don't want to handle the exception, you can simply ignore it by using the "pass" keyword.

Updated on: 22-Feb-2023

21K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements