howto catch an Exception and still print the TraceBack?

S

Saizan

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.
 
A

Antoon Pardon

Op 2006-02-01 said:
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)
 
D

Diez B. Roggisch

Saizan said:
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:

See

sys.exc_info()

The you can do:

try:
....
except: # catch all
_, e, tb = sys.exc_info()
print tb


Regards,

Diez
 
F

Fabio Zadrozny

I find the following very good for most needs:

try:
raise RuntimeError('err')
except:
import traceback;traceback.print_exc()

-- if you use Pydev, there's a template for that called printexc.

Cheers,

Fabio
 
S

Saizan

Thanks, I had completely missed the module traceback...
I'll use traceback.print_exc(), it seems the most straightforward way.
The only flaw is that the traceback starts in the method where i catch the exception and not from "__main__", but I guess it can't be helped.
 
F

Fabio Zadrozny

Saizan said:
Thanks, I had completely missed the module traceback...
I'll use traceback.print_exc(), it seems the most straightforward way.
The only flaw is that the traceback starts in the method where i catch the exception and not from "__main__", but I guess it can't be helped.
Actually, I guess that if you wanted to check the 'upper stack', you
could do it by checking sys._getframe() to get the current frame and
then go upwards with frame.f_back (that's how debbugers work), that way
you could get info on all the stacks you currently have... so if you
think it's worth it... ;-P

Cheers,

Fabio
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top