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

Paul Moore p.f.moore at gmail.com
Sat Jul 18 14:41:27 CEST 2009


2009/7/18 Michael Foord <fuzzyman at gmail.com>:
> Finally is normally dead simple but the explanation usually includes what it
> does with exceptions:
>
> Finally is *always* run last. Exceptions that occur in the try block are
> re-raised once the finally has completed. Exceptions inside the finally
> block will be re-raised
>
> The bit that has to be *added* in Python (and usually isn't added because it
> isn't at all obvious) is:
>
> Exceptions are not reraised if the finally returns or breaks.

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.

Paul.



More information about the Python-ideas mailing list