Retrieving a stacktrace from an exception object / forwarding an exception

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Fri Jun 22 05:05:40 EDT 2007


En Fri, 22 Jun 2007 03:54:12 -0300, Samuel <knipknap at gmail.com> escribió:

> I am trying to wrap a function that throws an exeption in such a way
> that the stacktrace is logged into a file and the exception is
> forwarded after that. For example:
>
> -------------------
> def my_func():
>     raise Exception("hello")
>
> def wrapper():
>     try:
>         my_func()
>     except Exception, e:
>         f = open("logfile", 'a')
>         f.write(e.stacktrace())
>         raise e
>
> wrapper() # should throw the exception with a stacktrace showing
> my_func()
> -------------------
>
> Any idea if and how this can be done?

- see the traceback module  
<http://docs.python.org/lib/module-traceback.html>
- use a bare raise (not raise e); this reraises the active exception  
without changing its context.

-- 
Gabriel Genellina




More information about the Python-list mailing list