preserve exception within try .. finally

Alvaro Figueiredo alvarof at freeshell.org
Fri Oct 10 16:25:21 EDT 2003


Em Sex 10 Out 2003 16:57, Brian Alexander escreveu:
> Hello;
>
> I'm curious to know how people preserve exceptions that arise
> in a try .. finally block. Consider this example:
>
> try:
>    getResource()
>    doSomething()
> finally:
>    alwaysFreeResource()
>
> If an exception occurs in doSomething(), the resource is freed
> anyway -- which is good. How can I keep the exception 'raised'
> for another try-finally/except to deal with? Does this problem
> reflect an error in the way I am approaching the problem?
>
>
> Many thanks,
>
> Brian.

I would do:

getResource()
try:
    doSomething()
finally:
    freeResource()

The exception keeps propagating until it is handled by a try: 
except: statement.

-- 
Alvaro Figueiredo
alvarof at freeshell.org





More information about the Python-list mailing list