1.Exception hierarchy
Every exception is a class inheriting from BaseException. Exception is one direct subclass of it, and almost everything you catch — ValueError, KeyError, OSError — descends from Exception, not BaseException directly.
Key features
- BaseException is the root of every exception; Exception is one of its direct subclasses
- SystemExit, KeyboardInterrupt, GeneratorExit inherit BaseException but NOT Exception
- except Exception: never catches those three — that separation is deliberate
- ArithmeticError, LookupError, OSError are broad subclasses grouping related errors
- A custom exception should inherit Exception, never BaseException directly
The core of the built-in hierarchy
- BaseException — the true root — every exception inherits from here
- Exception — the drawer for ordinary, handleable errors
- ArithmeticError / LookupError / OSError — broad groups within Exception
- ZeroDivisionError / KeyError / FileNotFoundError — the specific errors you actually catch
Command
BaseException
├── SystemExit, KeyboardInterrupt, GeneratorExit # NOT under Exception
└── Exception # catch this, not BaseException
├── ArithmeticError → ZeroDivisionError, OverflowError
├── LookupError → KeyError, IndexError
└── OSError → FileNotFoundError, PermissionError, ConnectionErrorexcept Exception excludes SystemExit/KeyboardInterrupt on purpose. Reach for BaseException only when you truly mean everything.