Interview Prep/Python for DevOps/Exceptions, context managers, files & logging

Why is a bare `except:` (or `except Exception: pass`) dangerous?

BeginnerConceptExceptions, context managers, files & logging
Answer

Bare except: catches BaseException, including KeyboardInterrupt and SystemExit, so Ctrl-C and sys.exit() stop working. except Exception: pass swallows every bug, typos included, and the script reports success while doing nothing. Catch the narrowest exception you can actually handle, log it with context, and re-raise anything you cannot handle. If you must catch broadly at the top level, log with log.exception(...) and exit non-zero.

All Python for DevOps questions Roadmap: Python for automation

More on Exceptions, context managers, files & logging