Exception Attributes
StopIteration.value — exception attribute
Read the return value carried when an iterator is exhausted. A generator return expression becomes this field; ordinary exhaustion generally carries None. For loops consume StopIteration internally. Raising StopIteration directly inside generator code is transformed into RuntimeError, so use return to finish a generator.
Formal Syntax & Structure
python syntaxpython
error.valueReturn Value
object | None
Generator result or first constructor argument.
Working Example
Example Snippetpython
def generate(): yield "item" return 42
iterator = generate()print(next(iterator))try: next(iterator)except StopIteration as error: print(error.value)