KeyError

Built-in ExceptionPython 2.0+Beginner

Raised when a dictionary key is not found

Quick Info

Documentation
Official Docs
Python Version
2.0+

Learn by Difficulty

Quick Example

python
# Triggering and catching KeyError
try:
    d = {}; d["missing"]
except KeyError as e:
    print(f"Caught KeyError: {e}")
    print(f"Type: {type(e).__name__}")

KeyError is raised when raised when a dictionary key is not found. Always catch specific exceptions rather than bare except clauses.

Try in Playground

Tags

exceptionerror-handlingcore