StopIteration

Built-in ExceptionPython 2.0+Intermediate

Raised by next() to signal that an iterator is exhausted

Quick Info

Documentation
Official Docs
Python Version
2.0+

Learn by Difficulty

Quick Example

python
# Triggering and catching StopIteration
try:
    it = iter([]); next(it)
except StopIteration as e:
    print(f"Caught StopIteration: {e}")
    print(f"Type: {type(e).__name__}")

StopIteration is raised when raised by next() to signal that an iterator is exhausted. Always catch specific exceptions rather than bare except clauses.

Try in Playground

Tags

exceptionerror-handlingcore