W3docs

How to properly ignore exceptions

It's generally not recommended to ignore exceptions, as they often indicate a problem in the code that should be addressed.

It's generally not recommended to ignore exceptions, as they often indicate a problem in the code that should be addressed. However, if you must ignore an exception, you can use a try-except block. For example:

Ignore all standard exceptions in a code block in Python

try:
    # code that might raise an exception
    1 / 0
except Exception:
    pass

This will catch the exception after it is raised and allow the program to continue executing the code after the try-except block.

However, it's important to know that catching all exceptions can hide bugs in your code. It's usually better to catch only the specific exceptions that you know how to handle and let the others propagate up the call stack. Using except Exception: is safer than a bare except: because it avoids catching system-level exceptions like SystemExit and KeyboardInterrupt. Ignoring exceptions is generally only acceptable in specific contexts, such as cleanup code or certain framework callbacks.

Ignore a specific exception in a code block in Python

try:
    # code that might raise an exception
    1 / 0
except ZeroDivisionError:
    pass

This will only catch the specific exception of ZeroDivisionError and the others will propagate up the call stack.