KeyError

The Python exception raised when you look up a dictionary key that does not exist.

In more detail

The error message names the missing key. Check first with in, use get(key, default) or a defaultdict, or catch KeyError when a missing key is expected.

Examples

v.get('order-id') or v.get('id')
In this course: In the course, rate_for raises a KeyError naming the unknown currency, and a mismatched gateway response makes pay fail with KeyError: 'id'. Pairing pytest.raises with match checks that the error is the one you meant.

Related terms

Taught in

References

Certification courses