Re-throwing an exception without loosing the stack trace

Clark C . Evans cce at clarkevans.com
Tue Feb 12 18:02:19 EST 2002


Some more detail as my previous post wasn't that clear.
I want to re-throw an exception and maintain the stack trace.

def very_nested():  
    ...
    barf
    ...

def somewhat_nested(): 
    ...
    very_nested()
    ..

def nested():
    try:
      ...
      somewhat_nested()
      ...
    except:  # requested incantation goes here
      database.close()
      raise 
    ...
    do stuff with database
    that is not closed
    ...

def main()
    ...
    nested()

When I run this, I want to know where "barf" came from.   
Right now, when I catch exceptions I get line 33 of
nested(), which is the raise statement --  I want
the line number of "barf".

I can't use finally since I want to continue 
using the database if it succeeds, but yes,
finally is close to what I want (the exception
passes through without having it's stack trace
screwed up)

Thanks!

Clark




More information about the Python-list mailing list