In section 7 of "What's New in Python 2.5" the
subsection on the close() generator method states:
close() raises a new GeneratorExit exception
inside the generator to terminate the iteration.
On receiving this exception, the generator's code
must either raise GeneratorExit or StopIteration;
catching the exception and doing anything else is
illegal and will trigger a RuntimeError.
This suggests that if a generator raises a new exception that is neither a GeneratorExit nor
StopIteration a RuntimeError is raised. But this
is not the case according to Part 4 of PEP 342's
"Specification Summary":
If the generator raises any other exception,
it is propagated to the caller.
The Python 2.5 interpreter is consistent with PEP 342:
Python 2.5 (r25:51908, Sep 19 2006, 09:52:17)
[MSC v.1310 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for
more information.
>>> def raise_wrong_exception():
... try:
... yield 1
... yield 2
... except GeneratorExit:
... raise TypeError("Where is the RuntimeError?")
...
>>> i=raise_wrong_exception()
>>> i.next()
1
>>> i.close()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 6, in raise_wrong_exception
TypeError: Where is the RuntimeError?
|