[Python-Dev] Problems with GeneratorExit deriving from Exception

Antoine Pitrou solipsis at pitrou.net
Sun Dec 2 14:56:13 CET 2007


(I was asked to forward this from the bug tracker)

> We have also run into problems where a task tries to "return" (yield Return())
> from within a try: except Exception: block.  Since returning from a coroutine is
> roughly equivalent to "raise GeneratorExit", the exception can be caught and
> ignored, with the same consequences as above.

I may be missing something but why don't you just catch StandardError
instead? If I believe Python 2.5's exception hierarchy it would catch
anything under Exception except for GeneratorExit, StopIteration and the
various Warnings.

Also it seems to me that muting any Exception is bad practice since it
can lead you to overlook errors in your code. It's better to catch a
specific Exception subclass, like IOError (or XMLRPCError, or whatever
it is called).


More information about the Python-Dev mailing list