IndexError

Built-in ExceptionPython 2.0+Beginner

Raised when a sequence index is out of range

Quick Info

Documentation
Official Docs
Python Version
2.0+

Learn by Difficulty

Quick Example

python
# Triggering and catching IndexError
try:
    lst = [1, 2, 3]; lst[10]
except IndexError as e:
    print(f"Caught IndexError: {e}")
    print(f"Type: {type(e).__name__}")

IndexError is raised when raised when a sequence index is out of range. Always catch specific exceptions rather than bare except clauses.

Try in Playground

Tags

exceptionerror-handlingcore