GeneratorExit

Built-in ExceptionPython 2.0+Advanced

Raised when a generator's close() method is called

Quick Info

Documentation
Official Docs
Python Version
2.0+

Learn by Difficulty

Quick Example

python
# Triggering and catching GeneratorExit
try:
    raise GeneratorExit("generator closed")
except GeneratorExit as e:
    print(f"Caught GeneratorExit: {e}")
    print(f"Type: {type(e).__name__}")

GeneratorExit is raised when raised when a generator's close() method is called. Always catch specific exceptions rather than bare except clauses.

Try in Playground

Tags

exceptionerror-handlingcore