# How to catch LookupError Exception in Python?

PythonServer Side ProgrammingProgramming

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

LookupError Exception is the Base class for errors raised when something can’t be found. The base class for the exceptions that are raised when a key or index used on a mapping or sequence is invalid: IndexError, KeyError.

An IndexError is raised when a sequence reference is out of range.

The given code is rewritten as follows to catch the exception and find its type

## Example

import sys
try:
foo = [a, s, d, f, g]
print foo[5]
except IndexError as e:
print e
print sys.exc_type

## Output

C:/Users/TutorialsPoint1~.py
list index out of range
<type 'exceptions.IndexError'>

Updated on 12-Feb-2020 10:54:12