Added brief section about exception propagation.
This commit is contained in:
parent
156af4640f
commit
195afdf22f
30
pep-0255.txt
30
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 "<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
|
||||
|
||||
# A binary tree class.
|
||||
|
|
Loading…
Reference in New Issue