Exceptions vs Errors in Python

  1. Yes. SyntaxError isn’t catchable except in cases of dynamically executed code (via eval/exec), because it occurs before the code is actually running.
  2. “Fatal” means “program dies regardless of what the code says”; that doesn’t happen with exceptions in Python, they’re all catchable. os._exit can forcibly kill the process, but it does so by bypassing the exception mechanism.
  3. There is no difference between exceptions and errors, so the nomenclature doesn’t matter.
  4. System-exiting exceptions derive from BaseException, but not Exception. But they can be caught just like any other exception
  5. Warnings behave differently based on the warnings filter, and deriving from Exception means they’re not in the “system-exiting” category
  6. AssertionError is just another Exception child class, so it’s not “system exiting”. It’s just tied to the assert statement, which has special semantics.
  7. Things deriving from BaseException but not Exception (e.g. SystemExit, KeyboardInterrupt) are “not reasonable to catch” (or if you do catch them, it should almost always be to log/perform cleanup and rethrow them), everything else (derived from Exception as well) is “conditionally reasonable to catch”. There is no other distinction.

To be clear, “system-exiting” is just a way of saying “things which except Exception: won’t catch”; if no except blocks are involved, all exceptions (aside from warnings, which as noted, behave differently based on the warnings filter) are “system-exiting”.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)