When should you define custom exception classes?
IntermediateConceptExceptions, context managers, files & logging
Answer
When callers need to react differently to different failures. A small hierarchy is enough:
class AuditError(Exception): """Base for this tool."""
class ConfigError(AuditError): ...
class PermissionDenied(AuditError): ...
The CLI entry point catches AuditError and maps subclasses to exit codes and friendly messages, while unexpected exceptions still produce a traceback. Do not create one class per call site; create them per decision a caller makes.
More on Exceptions, context managers, files & logging
How does `try` / `except` / `else` / `finally` work?BeginnerWhy is a bare `except:` (or `except Exception: pass`) dangerous?BeginnerWhat is exception chaining, and what is `raise ... from`?IntermediateWhat is the EAFP vs LBYL style, and which does Python prefer?IntermediateHow does a `with` statement work, and why use it for files?BeginnerHow do you write your own context manager?Advanced