diff --git a/pep-0255.txt b/pep-0255.txt index 597849f18..ae94aacb2 100644 --- a/pep-0255.txt +++ b/pep-0255.txt @@ -169,6 +169,36 @@ Specification functions. +Generators and Exception Propagation + + If an unhandled exception-- including, but not limited to, + StopIteration --is raised by, or passes through, a generator function, + then the exception is passed on to the caller in the usual way, and + subsequent attempts to resume the generator function raise + StopIteration. In other words, an unhandled exception terminates a + generator's useful life. + + Example (not idiomatic but to illustrate the point): + + >>> def f(): + ... return 1/0 + >>> def g(): + ... yield f() # the zero division exception propagates + ... yield 42 # and we'll never get here + >>> k = g() + >>> k.next() + Traceback (most recent call last): + File "", line 1, in ? + File "", line 2, in g + File "", line 2, in f + ZeroDivisionError: integer division or modulo by zero + >>> k.next() # and the generator function cannot be resumed + Traceback (most recent call last): + File "", line 1, in ? + StopIteration + >>> + + Example # A binary tree class.