Chapters
Python114 chapters

ReferenceException types

KeyError

A dictionary key that is not there.

Signature

KeyError(key)

Returns

Raised by [] on a mapping.

Example

Python
person = {"name": "Ada"}
try:
    person["age"]
except KeyError as problem:
    print("missing key:", problem)

print(person.get("age", "unknown"))

Output

missing key: 'age'
unknown

See also