Python LookupError Exception
Example
Catch both an IndexError and a KeyError with LookupError:
fruits = ["apple", "banana"]
car = {"brand": "Ford"}
try:
print(fruits[5])
except LookupError:
print("The list item does not exist")
try:
print(car["color"])
except LookupError:
print("The dictionary key does not exist")
Try it Yourself »
Definition and Usage
The LookupError exception is the base class for the errors that occur when a key or an index is not found.
IndexError (for lists, tuples and strings) and KeyError (for dictionaries) both inherit from LookupError, so except LookupError catches both of them.
Related Pages
Read more about handling errors in our Python Try Except Tutorial.