Built-in Exception Hierarchy
Python exceptions form a class hierarchy. BaseException is the root; Exception is the base for most errors. Knowing the hierarchy helps you catch at the right level.
Python exceptions form a class hierarchy. BaseException is the root; Exception is the base for most errors. Knowing the hierarchy helps you catch at the right level.
# Common hierarchy:
# BaseException
# SystemExit, KeyboardInterrupt, GeneratorExit
# Exception
# ValueError, TypeError, KeyError, IndexError ...
try:
raise KeyError("missing")
except LookupError: # parent of KeyError
print("Lookup problem")
Catch LookupError to handle both KeyError and IndexError.