Re: About python 2.5 and its try statement.



Hi,


defcon8 wrote:
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
function-exit.

(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.)

Clearer?

Cheers,

--Tim

.



Relevant Pages

  • Re: decorators tutorials
    ... I am learning python by learning django, ... allow the SystemExit exception to continue to propogate without doing ... # The func argument is a Pythong function object ... trying to get the result of DivXY, ...
    (comp.lang.python)
  • Re: math.nroot [was Re: A brief question.]
    ... >>> wish sometimes that Python would make up it's mind about what it does ... >> exception on overflow, invalid operation, and divide by 0, and "should ... >> not", by default, raise an exception on underflow or inexact. ... you can at least be pretty sure that an infinite result is the ...
    (comp.lang.python)
  • Re: exceptions
    ... How do younger languages like IO make this point more forcefully than lisp, ... deal with *all* errors in an interactive session, ... certainly in python ... > exception handling is hard-coded in syntax. ...
    (comp.lang.python)
  • Re: thread, threading; how to kill a thread?
    ... Python is embedded into C++. ... I was able to kill the threads ... nThreadId is the ID of the thread that you want to terminate (i.e. ... Calling this function will cause an exception to ...
    (comp.lang.python)
  • Re: exceptions
    ... Io was based on experience from lots of other languages. ... One typical example mentioned in AOP is precisely exception handling. ... > certainly in python ... just too bad that the end user cannot override internal C/assembler ...
    (comp.lang.python)