In software development, anomalous execution events are divided into two categories:
- Syntax Errors (Parsing Errors): Violations of Python's formal grammar (e.g., missing colons, mismatched parentheses, invalid indentation). Caught by the tokenizer/parser *prior* to program execution. Execution cannot begin until all syntax errors are resolved.
- Exceptions (Runtime Errors): Disruptive events that occur *during* program execution when syntactically valid code attempts an illegal operation (e.g., dividing by zero, opening a non-existent file, indexing an empty list). If not caught by an exception handler, the Python interpreter halts execution and prints a diagnostic Traceback.
The Python Built-in Exception Hierarchy
All Python exceptions are classes organized in a strict object-oriented inheritance tree rooted at BaseException:
BaseException
├── SystemExit (Triggered by sys.exit())
├── KeyboardInterrupt (Triggered by Ctrl+C in terminal)
└── Exception (Root superclass for all non-fatal application exceptions)
├── ArithmeticError
│ ├── ZeroDivisionError (e.g., 10 / 0)
│ └── OverflowError (Calculation exceeds floating-point limits)
├── LookupError
│ ├── IndexError (Sequence index out of range)
│ └── KeyError (Dictionary key not found)
├── ValueError (Correct type, but invalid value, e.g., int("abc"))
├── TypeError (Operation applied to inappropriate data type)
├── NameError (Identifier not found in any LEGB scope)
└── OSError
├── FileNotFoundError (File path does not exist)
└── PermissionError (Insufficient OS file access rights)