Exceptions
Common built-in exception types you will see while learning Python.
| Exception | Typical cause |
|---|---|
SyntaxError | Invalid Python syntax |
NameError | Name not defined |
TypeError | Wrong type for an operation |
ValueError | Right type, wrong value |
IndexError | Sequence index out of range |
KeyError | Dict key missing |
AttributeError | Missing attribute / method |
ZeroDivisionError | Division by zero |
FileNotFoundError | File path does not exist |
ImportError / ModuleNotFoundError | Import failed |
StopIteration | Iterator exhausted |
Exception | Base for most errors |
Example
try:
print({"a": 1}["b"])
except KeyError as e:
print("missing key:", e)