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
try:
save_to_db(record)
except:
passcan hide a genuine `TypeError` from a typo in the code for months, since the failure never surfaces anywhere.