Exceptions

Common built-in exception types you will see while learning Python.

ExceptionTypical cause
SyntaxErrorInvalid Python syntax
NameErrorName not defined
TypeErrorWrong type for an operation
ValueErrorRight type, wrong value
IndexErrorSequence index out of range
KeyErrorDict key missing
AttributeErrorMissing attribute / method
ZeroDivisionErrorDivision by zero
FileNotFoundErrorFile path does not exist
ImportError / ModuleNotFoundErrorImport failed
StopIterationIterator exhausted
ExceptionBase for most errors
Example
try:
    print({"a": 1}["b"])
except KeyError as e:
    print("missing key:", e)