About python 2.5 and its try statement.
Tim N. van der Leeuw
tim.leeuwvander at nl.unisys.com
Mon Jun 26 14:43:08 CEST 2006
> I can't remember the proposal number, but many of you reading will have
> probably read the features that will be added to python 2.5. The actual
> part I wanted to talk about was the finally part of try. Isn't it
> totally defeating a compiler's job by executing the finally part even
> if there is an error in the previous statements? Or have I understood
> something wrong?
try ... finally already exists in current python; what's new is that it
can be combined in 1 statement with try ... except ... finally.
No, this is not defeating a compilers job. It's very useful.
It defines an amount of cleanup that needs to happen no matter what,
exception or no exception. Example: closing an open file. You'll want
to close the file, whether after you done the job you intended to do,
or after an exception occurred while doing something.
So you put 'f.close()' in your finally - suite.
Another example of a useful 'finally' is logging function-exit -- not
the actual 'return' statement, but just a log-statement recording
(Putting a 'return' statement in a 'finally' suite is totally defeating
the point of exceptions that you want thrown to the caller, yes. So
don't put your 'return' statement there.)
More information about the Python-list