Manually printing traceback?

David Bolen db3l at fitlinxx.com
Tue Aug 7 23:56:42 EDT 2001


sill at optonline.net (Andrei Kulakov) writes:

> Hello,
> 
> If I run this code: l = []; print l[1], I get a traceback.
> If I change it to:
> 
> import sys
> l = []
> try: print l[1]
> except:
>     print sys.exc_info()
> 
> I get a tuple, 3rd element of which is the traceback object. How do i print
> it? I tried doing print sys.exc_info()[2]() but that doesn't work. I also
> read libref and langref and i couldn't find anything enlightening on this.
> I found a post where paul prescott asks a similar question and gvr tells him
> that you basically have to read code for some debugger, or something of this
> sort.. that's over my head, i'm afraid.

Depending on what you want to do, you could:

    import traceback

    try:
        print l[1]
    except:
        traceback.print_exc()

This will print a standard traceback to sys.stderr.  You can adjust
where the traceback goes by adding a "file=xxx" parameter to the
print_exc() call where xxx is a file object.  You can also specify the
depth of the traceback as a parameter.  The traceback is automatically
generated from the information in sys.exc_info(), which is why it's
easy to use directly in an exception handler.  There are other print_*
functions in the traceback module for finer grained control over the
exception information it decodes.

Or if you are trying to get traceback information in a readable form
to use or transfer elsewhere, there are format_* functions in the
traceback module that yield tracebacks as strings which you can then
manage separately.  So an replacement to the above might be:

    import sys, traceback

    try:
        print l[1]
    except:
	msg = apply(traceback.format_exception,sys.exc_info())
	sys.stderr.writelines(msg)

although in such a case you'd probably use 'msg' (which is a list of
strings) to save or transfer the formatted traceback elsewhere rather
than just writing it to stderr.

> The reason i ask is that my program (cymbaline) doesn't print out exceptions
> that it raises. I have no idea why.. I don't have any big chunks in try:
> except: catch-all blocks.. So, alternatively, if i knew what else could be
> causing this, i wouldn't need the answer the my first question. I just need
> the answer to either one :P

Answering this is a lot harder - can you provide more information on
the environment within which your application is running, since some
may suppress tracebacks (e.g., CGI scripts, running without a console
under Windows).  Or perhaps you just have stderr from your script
going elsewhere or accidentally discarded ("2>/dev/null" or
something)?

--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



More information about the Python-list mailing list