Newbie: Capture traceback message to string?

Chris Mellon arkanes at gmail.com
Wed Jan 17 16:54:41 CET 2007


On 17 Jan 2007 15:31:03 GMT, Thomas Guettler
<guettli.usenet at thomas-guettler.de> wrote:
> Sean Schertell wrote:
>
> > Hello!
> >
> > I'm new to Python and this is my first post to the list.
> >
> > I'm trying to simply capture exception text to a few strings which
> > can be passed to a PSP page to display a pretty error message. The
> > problem is that I just can't seem to figure out how to get the basic
> > components of the traceback message into strings. Here's what I want
> > to do:
> >
>
>
> # http://www.thomas-guettler.de/vortraege/python/beispiele.py.txt
> def foo():
>     raise("Das ist eine Exception")
> try:
>     foo()
> except:
>     import traceback
>     import cStringIO
>     (exc_type, exc_value, tb) = sys.exc_info()
>     exc_file = cStringIO.StringIO()
>     traceback.print_exception(exc_type, exc_value, tb, file=exc_file)
>     exc_string=exc_file.getvalue()
>     print exc_string
>

This is a lot of extra work. traceback.format_exc is much simpler.



More information about the Python-list mailing list