How do you properly ignore Exceptions in Python?

Ignoring exceptions in Python can be done using try-except blocks with a pass statement. Here are the proper approaches ?

Method 1: Using Bare except

This approach catches all exceptions, including system-level exceptions ?

try:
    x, y = 7, 0
    z = x / y
    print(f"Result: {z}")
except:
    pass
    
print("Program continues...")

The output of the above code is ?

Program continues...

Method 2: Using except Exception

This is the recommended approach as it only catches regular exceptions ?

try:
    x, y = 7, 0
    z = x / y
    print(f"Result: {z}")
except Exception:
    pass
    
print("Program continues...")

The output of the above code is ?

Program continues...

Key Differences

The difference between the above codes is that the first one will also catch KeyboardInterrupt, SystemExit etc, which are derived directly from exceptions.BaseException, not exceptions.Exception. Using except Exception: is generally preferred as it allows system-level exceptions to propagate normally.

Memory Considerations

It is known that the last thrown exception is remembered in Python, and some of the objects involved in the exception-throwing statement are kept live until the next exception. In older Python versions (Python 2), you might want to clear the exception reference ?

import sys

try:
    x, y = 7, 0
    z = x / y
except Exception:
    sys.exc_clear()  # Only available in Python 2

Note: sys.exc_clear() was removed in Python 3 as the garbage collector handles exception cleanup automatically.

Conclusion

When ignoring exceptions in Python, use except Exception: instead of bare except: to avoid catching system-level exceptions. The pass statement effectively ignores the exception and allows program execution to continue.

Updated on: 2026-03-13T17:45:18+05:30

629 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements