howto catch an Exception and still print the TraceBack?

Antoon Pardon apardon at forel.vub.ac.be
Wed Feb 1 10:00:10 EST 2006


Op 2006-02-01, Saizan schreef <sanzhiyan at gmail.com>:
> In an event-driven application i'd like to keep the program alive regardless of any exceptions raised by the handlers,
> but still be able to debug them by reading the appropriate TraceBack from stderr.
> I can put something like:
>
> try:
> 	self.call_handler(handler,*args)
> except Exception, e:
> 	print e
> 	print e.args
>
> in the dispatcher, but that isn't as helpful as a complete TraceBack.

You mean something like this?

    import traceback
    import sys

    try:
        self.call_handler(handler,*args)
    except Exception, e:
        Do_whatever_you need_to_do()
        for msg in traceback.format_tb(sys.exc_info()[2]):
            sys.stderr.write("%s\n" % msg)



More information about the Python-list mailing list