473,785 Members | 2,363 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_handl er(handler,*arg s)
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 1595
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*******@gmai l.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_handl er(handler,*arg s)
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_handl er(handler,*arg s)
except Exception, e:
Do_whatever_you need_to_do()
for msg in traceback.forma t_tb(sys.exc_in fo()[2]):
sys.stderr.writ e("%s\n" % msg)
Feb 1 '06 #3
I find the following very good for most needs:

try:
raise RuntimeError('e rr')
except:
import traceback;trace back.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_handl er(handler,*arg s)
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
16299
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 is an example. <TRACEBACK> Traceback (most recent call last):
4
2952
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
6129
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 understand Finally runs whether an error was caught or not, I haven't found a use for finally yet.
3
5563
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 defined".
6
2496
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 locked, I want a messagebox to tell that the user has to close the file first. Is there a way to identify an exception by some kind of unique number or something like this? I don't want to identify it on the Exception Message because some users are...
3
1255
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 = False
5
1935
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 simply this - how can a function determine whether or not it's being called in handling of an exception (ie. there is an "active" exception, and somewhere upstream on the stack there is an except: block statement)? Depending on what you read,...
2
1967
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 simple piece of code that I typed in to test this fact: --------------------------------------------------------------------------- import sys
0
792
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
9647
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10161
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9958
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7506
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5523
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4058
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3662
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2890
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.