Ignore an Exception in Python

  1. Use the pass Statement in the except Block in Python
  2. Use the sys.exc_clear() Statement in the except Block in Python

An exception is an event that, when raised, alters the flow of the program.

Exceptions result from the program being syntactically correct but still giving an error on the execution of the code. This error does not halter the program’s execution but rather changes the default flow of the program.

In Python, we handle exceptions using the try...except block. This tutorial will discuss several methods to ignore an exception and proceed with the code in Python.

Use the pass Statement in the except Block in Python

The pass statement can be considered as a placeholder in Python programming. It returns a NULL statement and, therefore, produces no value. However, the Python interpreter does not ignore the pass statement, and we prevent getting errors for empty code where the statement is used.

When the pass statement is used in the try...except statements, it simply passes any errors and does not alter the flow of the Python program.

The following code uses the pass statement in the except block to ignore an exception and proceed with the code in Python.

try:
    print(hey)
except Exception:
    pass

print("ignored the exception")

The above code provides the following output.

ignored the exception

Although this function always works in Python 3 and above, using the pass statement is considered a bad programming practice. It doesn’t provide a solution to the errors that might arise during the program. Moreover, identifying the errors in a given program is much more difficult as it ignores every single error.

Use the sys.exc_clear() Statement in the except Block in Python

In Python 2, the last thrown exception gets remembered by the interpreter, while it does not happen in the newer versions of Python. Therefore, the sys.exc_clear() statement is not needed in the versions released after Python 3. The sys.exc_clear() statement can be utilized to clear the last thrown exception of the Python interpreter.

The following code uses the sys.exc_clear() statement in the except block to ignore an exception and proceed with the code in Python.

try:
    print(hey)
except Exception:
    sys.exc_clear()

print("ignored the exception")

Although these two ways manage to make the program run without any errors, it is not recommended to ignore all the errors in a program. However, ignoring only a particular error or some errors is a practice that most programmers do for a healthy program.

Limiting the use of the pass statement and sys.exc_clear() statement in a program also improves the readability and identification of errors of a program.

Write for us
DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Python Exception

  • Raise Exception in Python
  • Handle NameError Exception in Python
  • Python except Exception as e
  • Python Assert Exception