Skip to content

How to Ignore Errors and Exceptions in Python with 'try...except pass'

Learn how to use try...except pass in Python to intentionally ignore specific exceptions while avoiding the pitfalls of bare except blocks that catch all errors.

While the primary purpose of try...except blocks in Python is to handle errors gracefully, sometimes you might want to intentionally ignore certain exceptions, allowing your program to continue even if a specific operation fails.

This guide explains how to use the pass statement within an except block to achieve this, and crucially, why it's important to catch specific exceptions rather than using a bare except:.

Ignoring Exceptions with except: pass (Use with Caution)

You can use a bare except: block followed by the pass statement to catch and effectively ignore any exception that occurs within the try block.

my_list = []

try:
    print(my_list[100])
except:
    pass

print("Program continues after the try...except block.") # This will be printed
  • The try block attempts to execute the code within it.
  • If any exception occurs (like IndexError or ValueError in the examples below), the except: block catches it.
  • The pass statement is a null operation, i.e. it does nothing. The program simply continues executing after the try...except block.
my_list = []
try:
    my_list.remove(1000) # 1000 is not in the list
except:
    pass # Ignore the ValueError
print("Program still continues.")

The better practice is to explicitly catch only the specific exception(s) you anticipate and want to ignore:

my_list = []

try:
    print(my_list[100])
except IndexError:
    pass

print("Program continues...") # This will be printed

try:
    result = 25 / 0 # This will raise a ZeroDivisionError
except IndexError:
    pass
    
print("This line might not be reached if ZeroDivisionError is unhandled.")
  • By specifying except IndexError:, you ensure that only IndexError exceptions are caught and ignored.
  • Other unexpected errors (like ZeroDivisionError in the second try block) will still be raised, alerting you to potential problems.

Ignoring Multiple Specific Exceptions

You can catch multiple specific exceptions in a single except block by providing them as a tuple:

my_list = []

try:
    print(25 / 0)
except (IndexError, ZeroDivisionError):
    pass

print("Program continues...") # This will be printed
  • The except (IndexError, ZeroDivisionError): block will catch either an IndexError or a ZeroDivisionError, but no other types of exceptions.

When to Ignore Exceptions (and When Not To)

Ignoring exceptions with pass should be done cautiously. Legitimate use cases include:

  • Optional Operations: When an operation might fail, but the failure isn't critical to the main flow (e.g., attempting to delete a file that might not exist).
  • Resource Cleanup (in finally): Sometimes pass might appear in a finally block's except clause during cleanup, although more specific logging is usually better.

Avoid ignoring exceptions when:

  • You don't understand why the exception might occur.
  • The exception indicates a serious problem that should be fixed.
  • You could handle the exception more gracefully (e.g., by providing a default value, logging the error, or retrying).
Navigation