Search GimmeJob

Type at least 2 characters. Enter also searches by meaning.
Python interview question
Errors & context managersJuniorCommonTroubleshooting

Why is a bare `except:` (or `except Exception: pass`) considered an anti-pattern?

Answer

A bare `except:` is the more dangerous of the two: it catches every exception without distinction, including `BaseException` subclasses like `KeyboardInterrupt` and `SystemExit`, so it can swallow a user's Ctrl+C or a deliberate `sys.exit()` right alongside real bugs. `except Exception: pass` is narrower — `KeyboardInterrupt` and `SystemExit` inherit directly from `BaseException`, not `Exception`, so it does not catch those — but it still indiscriminately catches every ordinary error, including programming mistakes like `TypeError` or `AttributeError` from a genuine bug, hiding real defects behind silent success instead of surfacing them. Combined with `pass`, either form actively destroys the diagnostic information (the traceback) that would have helped find the actual problem. Correct practice catches the narrowest exception type the code can meaningfully recover from, and if broader logging is truly needed, logs the full exception with its traceback before deciding whether to re-raise.

Strong answer includes

  • bare except: also catches BaseException subclasses like KeyboardInterrupt/SystemExit; except Exception: does not
  • both forms combined with pass destroy diagnostic traceback information
  • correct practice catches the narrowest recoverable exception type

Practical example

Python example
try:
    save_to_db(record)
except:
    pass

can hide a genuine `TypeError` from a typo in the code for months, since the failure never surfaces anywhere.

#exceptions#anti-patterns

Sources

Errors and ExceptionsPython Software Foundation · try/except/else/finally control flow and exception chainingPEP 8 — Style Guide for Python CodePython Software Foundation · Naming, layout and readability conventions