Python follows EAFP — "easier to ask forgiveness than permission": try the operation and handle the exception, rather than checking every condition first. Full syntax Raising raise ValueError("message") raise AppError("context") from err — keeps the original as cause Re-raise the current exception with a bare raise. Custom exceptions Create a base class for your package (class AppError(Exception)) and specific subclasses. Callers can catch the whole family or just one. Common built-ins ValueError, TypeError, KeyError, IndexError, FileNotFoundError, ZeroDivisionError, AttributeError, TimeoutError. Never use a bare except: — it also catches KeyboardInterrupt and SystemExit. Catch Exception at most, and only at top-level boundaries.
Key Takeaways
Catch specific exceptions; never use a bare except.
try/except/else/finally: else runs on success, finally always.
raise … from err preserves the original cause.
Give your package one base exception class.
except handles ExceptionGroups from concurrent code (3.11+).