473,412 Members | 2,072 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

howto catch an Exception and still print the TraceBack?

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.
Feb 1 '06 #1
5 1581
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

Feb 1 '06 #2
Op 2006-02-01, Saizan schreef <sa*******@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)
Feb 1 '06 #3
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.

Feb 1 '06 #4
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.
Feb 2 '06 #5
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
Feb 2 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Anand Pillai | last post by:
Hi I am quite familiar with normal python errors which can be caught by using the try... except... finally clause. But very often I find other kinds of exceptions raised in my programs. Here...
4
by: Brian Alexander | last post by:
Hello; I'm curious to know how people preserve exceptions that arise in a try ... finally block. Consider this example: try: getResource() doSomething() finally: alwaysFreeResource()
32
by: cj | last post by:
Another wish of mine. I wish there was a way in the Try Catch structure to say if there wasn't an error to do something. Like an else statement. Try Catch Else Finally. Also because I...
3
by: Shuaib | last post by:
Hey! I am getting this exception. xml.parsers.expat.ExpatError But I am not able to catch it with "except xml.parsers.expat.ExpatError:" It says "NameError: global name 'xml' is not...
6
by: Pieter | last post by:
Hi, For some procedures that throws exceptions, I would like to show different messages to the user depending on what type of exception he's getting. For instance this one: when the file is...
3
by: Thomas Dybdahl Ahle | last post by:
Hi, I have a function, which looks like the following: connecting = False def func (): global connecting connecting = True try: # Do lot of network stuff except Exception, e: connecting =...
5
by: NeBlackCat (lists) | last post by:
Hello everybody - my first post! And it may be the most monumentally stupid question ever asked, but I just can't see an answer after several hours experimenting, searching and reading. It's...
2
by: Sami | last post by:
Hello, In the Python book that I am using to learn the language it says that the traceback.print_exc() can be used to stop exception propagation and make the program keep running. Here is a...
0
by: mk | last post by:
Robert Rawlins wrote: Sure. I'm a little confused as to how I can modify that Remember, Google is your friend, here's the crux of the method without the fancy schmancy sugar coating:
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.