[Python-ideas] Make return inside a finally a SyntaxError

Nick Coghlan ncoghlan at gmail.com
Sat Jul 18 15:54:07 CEST 2009


Paul Moore wrote:
> While I'll concede that it's a bit subtle, the finally doesn't
> "complete" if you return or break - those two statements cause
> premature exit. As such, it's quite right that the exception isn't
> re-raised in those cases, as the finally clause never "completed".
> 
> Subtle, yes. Inconsistent, no.

This is the reason the current semantics make sense to me as well:
return, break and raise all mean that the finally block doesn't actually
finish, so the implicit "re-raise" at the end of the finally block is
never executed.

If anything was going to be made more consistent, I would suggest it
would be to allow continue in a finally statement with similar semantics
to break. I suspect there may be implementation factors that would make
that difficult though.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------



More information about the Python-ideas mailing list