StopAsyncIteration — Easy Examples
Raised by __anext__() to signal an async iterator is exhausted
Triggering StopAsyncIteration
How StopAsyncIteration is raised and how to catch it.
python
# Triggering and catching StopAsyncIteration try: raise StopAsyncIteration("async iteration done") except StopAsyncIteration as e: print(f"Caught StopAsyncIteration: {e}") print(f"Type: {type(e).__name__}")
StopAsyncIteration is raised when raised by __anext__() to signal an async iterator is exhausted. Always catch specific exceptions rather than bare except clauses.
Handling StopAsyncIteration
Basic error handling pattern for StopAsyncIteration.
python
# Safe handling pattern def safe_operation(): try: raise StopAsyncIteration("async iteration done") except StopAsyncIteration: print("Operation failed gracefully") return None result = safe_operation() print(f"Result: {result}")
Wrapping risky operations in try/except blocks prevents your program from crashing.
Want to try these examples interactively?
Open Easy Playground