[Python-3000] self-contained exceptions

Mike Orr sluggoster at gmail.com
Thu Jan 4 22:22:39 CET 2007


On 1/4/07, tomer filiba <tomerfiliba at gmail.com> wrote:
> [Guido]
> > The use case I am guessing from your example (passing a
> > traceback as a single string across an RPC boundary) isn't all that
> > common and you ought to have only one place in your RPC package where
> > you need to call it.
>
> *this* use-case isn't that useful, true. but many times, frameworks need
> to store the exception details "for later", for example, unit-testing.
> you want to run the entire test, and print a report later.

Except that different applications want them formatted differently.
No stack trace, max N levels stack trace, etc.  I frequently use
"Caught exception_name: exception_value" when looping over records
from a file so I can see all the exceptions for all records rather
than aborting at the first one.  Yes, one *can* use

    "%s: %s" % (str(e.__class), str(e)

and sometimes I do, but it looks like boilerplate code that should be
handled in the stdlib somewhere.  The traceback module almost does
what I want:

    traceback.format_exception_only(e.__class__, e)  ->  list of strings

Though I really wish I didn't have to pass the arguments.  If
traceback.format_exc() could have an argument that suppresses the
"Traceback (most recent call last):", that would also work.  (Why not
use None for this as print_tb does?)

Of course, having two or three formatting methods off the exception
object would also be fine.  The point is that one universal format is
insufficient, especially if it includes a long traceback.

-- 
Mike Orr <sluggoster at gmail.com>


More information about the Python-3000 mailing list