Why no try-except-finally ?

Frithiof Andreas Jensen frithiof.jensen at removethis.ted.ericsson.dk
Wed Nov 5 04:30:17 EST 2003


"KefX" <keflimarcusx at aol.comNOSPAM> wrote in message
news:20031105040556.29079.00000209 at mb-m22.aol.com...


> this idiom unPythonic somehow? (The nested-try thing does look odd...) Or
is it
> the way we're supposed to do it? Or is there something I'm missing?

According to the documentation the purpose of a try...finally statement is
to ensure that aquired ressources will be freed again as in

self.lock.acquire()
try:
    ..do whatever we are supposed to do;
    ...it may fail here or in a different module etc.
    ...exceptions may be caught or not
finally:
    ...but we always end *here*
    ...and uncaught exceptions are re-raised from here too (i think)
    ...so that "the right thing" happens
    self.lock.release()

so that the ressource is released regardless of any exceptions etc. that
happens between the try: and the finally:

The separation is probably because the purpose of try..finally is different
from exception handling and mixing the two would obscure that (or maybe
there is some wizardry under the hood).






More information about the Python-list mailing list