Added brief section about exception propagation.

This commit is contained in:
Tim Peters 2001-06-20 19:08:20 +00:00
parent 156af4640f
commit 195afdf22f
1 changed files with 30 additions and 0 deletions

View File

@ -169,6 +169,36 @@ Specification
functions. 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 "<stdin>", line 1, in ?
File "<stdin>", line 2, in g
File "<stdin>", 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 "<stdin>", line 1, in ?
StopIteration
>>>
Example Example
# A binary tree class. # A binary tree class.