2009/7/18 Stephen J. Turnbull <stephen@xemacs.org>
Greg Ewing writes:
 > Michael wrote:
 > > Here are two examples of why allowing return inside a finally block is
 > > a bad idea:
 > Don't do those things, then!
 > Nobody is *forcing* you to write returns inside
 > finally blocks.


Why not just emphasize in the documentation that return (etc) in a
finally: suite *will* get executed *after* return or an exception is
raised in the try: suite?  

That exceptions can be silently swallowed in a finally block (which is 'expected' and usually intended to propagate exceptions) in the presence of a return (or a break apparently) is worrying.

Another solution would be to have the exception raised instead of swallowed. There is a harder migration regarding backwards compatibility though - you can only warn when the exception is swallowed which may never be seen by the programmer.


"These stunts were performed by
professionals.  Don't try this at home, kids."

More specifically, maybe there should be an explicit warning that in a
finally: suite

   if exit_condition:
   # end of suite

has (perhaps surprisingly, YMMV) different semantics from

   if not exit_condition:
   # end of suite

Python-ideas mailing list