Frank B <> Wrote in message:
> Ok; this is a bit esoteric.
> So finally is executed regardless of whether an exception occurs, so states 
> the docs.
> But, I thought, if I <return> from my function first, that should take 
> precedence.
> au contraire
> Turns out that if you do this:
> try:
>   failingthing()
> except FailException:
>   return 0
> finally:
>   return 1
> Then finally really is executed regardless... even though you told it to 
> return.
> That seems odd to me.

The thing that's odd to me is that a return is permissible inside
 a finally block. That return
should be at top level,  even with the finally line. And of course
 something else should be in the body of the finally

If you wanted the finally block to change the return value,  it
 should do it via a variable.

retval = 0 
except FailException:
     return retval
     retval =1 
return something

I imagine the finally clause was designed to do cleanup, like
 closing files.  And it certainly predated the with statement.



Reply via email to