preserve exception within try .. finally

Gonçalo Rodrigues op73418 at mail.telepac.pt
Fri Oct 10 16:25:30 EDT 2003


On Fri, 10 Oct 2003 15:57:13 -0400, Brian Alexander
<brian094 at sympatico.ca> wrote:

>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?
>

Maybe I'm not understanding your question but the finally block does
*not* handle the exception. The exception propagates:

>>> try:
... 	raise Exception
... finally:
... 	print "I warned you"
... 	
I warned you
Traceback (most recent call last):
  File "<interactive input>", line 2, in ?
Exception
>>> 

HTH,
G. Rodrigues




More information about the Python-list mailing list