Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > howto catch an Exception and still print the TraceBack?

Reply
Thread Tools

howto catch an Exception and still print the TraceBack?

 
 
Saizan
Guest
Posts: n/a
 
      02-01-2006
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.
 
Reply With Quote
 
 
 
 
Antoon Pardon
Guest
Posts: n/a
 
      02-01-2006
Op 2006-02-01, Saizan schreef <(E-Mail Removed)>:
> 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)
 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      02-01-2006
Saizan wrote:

> 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



 
Reply With Quote
 
Fabio Zadrozny
Guest
Posts: n/a
 
      02-01-2006
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

Saizan wrote:

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



 
Reply With Quote
 
Saizan
Guest
Posts: n/a
 
      02-02-2006
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.
 
Reply With Quote
 
Fabio Zadrozny
Guest
Posts: n/a
 
      02-02-2006
Saizan wrote:

>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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
catch doesn't catch a thrown exception Marteno Rodia Java 5 08-05-2009 03:30 AM
Howto rethrow an exception outside a catch block? reuce-google@yahoo.de C++ 6 08-09-2007 08:38 AM
Problem - I want to print Current Output of Pdf file and should print once.I get print dialog box but it is not working keto Java 0 05-30-2007 11:27 AM
howto get exception information from asp.net webservice, without try and catch dan ASP .Net 2 05-25-2005 06:27 AM
why catch (...) can not catch such exception John Black C++ 8 08-20-2004 02:34 PM



Advertisments