Built-in Exceptions

KeyError — exception class

A mapping lookup requests a key that is absent. For ordinary dict objects, use get() when a fallback is appropriate; a present key storing None is not absent. The requested key is commonly in args[0]. Catch KeyError when this category is the recovery boundary; do not rely on the exact human-readable message.

Formal Syntax & Structure

python syntaxpython
KeyError

Return Value

type

The class object KeyError; this reference documents the class identity, not a universal constructor signature.

Working Example

Example Snippetpython
data={"ready":None}try:    data["missing"]except KeyError as error:    print(type(error).__name__)
Python Reference