How come StopIteration.__base__ is not BaseException?

Marco Buttu marco.buttu at gmail.com
Tue Aug 27 15:52:35 EDT 2013


On 08/27/2013 08:51 PM, Terry Reedy wrote:

> BaseException was added just so it would be possible to catch nearly
> everything but a few exceptions. The first two were KeyboardInterrupt
> and SystemExit (in 2.5). GeneratorExit was switched in 2.6, but I forget
> the details of why.

Maybe in order to don't catch it inside a generator using a except 
Exception clause, because it is used to notify an active generator is 
closed:

 >>> def foogen():
...     for i in range(10):
...         try:
...             yield i
...         except:
...             print('Catched!')
...             # raise
...
 >>> g = foogen()
 >>> next(g)
0
 >>> g.close()
Catched!
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
RuntimeError: generator ignored GeneratorExit

Do you remember if this is the reason? Thanks,
-- 
Marco



More information about the Python-list mailing list