473,549 Members | 2,733 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Overriding traceback print_exc()?

I want to cause any traceback output from my applications to show up in one
of my dialog boxes, instead of in the command or terminal window (between
running on Solaris, Linux, OSX and Windows systems there might not be any
command window or terminal window to show the traceback messages in). Do I
want to do something like override the print_exc (or format_exc?) method of
traceback to get the text of the message and call my dialog box routine? If
that is right how do I do that (monkeying with classes is all still a grey
area to me)?

I kind of understand using the traceback module to alter the output of
exceptions that I am looking for, but I'm after those pesky ones that should
never happen. :)

Thanks!

Bob
Oct 31 '06 #1
5 3638
Bob Greschke wrote:
I want to cause any traceback output from my applications to show up in one
of my dialog boxes, instead of in the command or terminal window (between
running on Solaris, Linux, OSX and Windows systems there might not be any
command window or terminal window to show the traceback messages in). Do I
want to do something like override the print_exc (or format_exc?) method of
traceback to get the text of the message and call my dialog box routine?
one way to do that is to put a big try/except around your main program,
and display the dialogue box in the except clause:

import traceback

def main():
raise RuntimeError("o ops!")

try:
main()
except (KeyboardInterr upt, SystemExit):
raise
except:
print "ERROR", repr(traceback. format_exc())

another approach is to install an exit-handler that uses last_traceback
and friends to generate a traceback:

import atexit, traceback, sys

def postmortem():
if hasattr(sys, "last_traceback "):
print "ERROR", repr(traceback. format_exceptio n(
sys.last_type, sys.last_value, sys.last_traceb ack
))

atexit.register (postmortem)

def main():
raise RuntimeError("o ops!")

main()

the latter is less intrusive, and can be squirreled away in a support
module. also, the original exception is still reported to the console,
as usual.

</F>

Oct 31 '06 #2
You could always override sys.stderr with a instance of your own with a
write() method. Though you will still have to catch the exceptions.

Bob Greschke wrote:
I want to cause any traceback output from my applications to show up in one
of my dialog boxes, instead of in the command or terminal window (between
running on Solaris, Linux, OSX and Windows systems there might not be any
command window or terminal window to show the traceback messages in). Do I
want to do something like override the print_exc (or format_exc?) method of
traceback to get the text of the message and call my dialog box routine? If
that is right how do I do that (monkeying with classes is all still a grey
area to me)?

I kind of understand using the traceback module to alter the output of
exceptions that I am looking for, but I'm after those pesky ones that should
never happen. :)

Thanks!

Bob
Oct 31 '06 #3
Bob Greschke wrote:
I want to cause any traceback output from my applications to show up in one
of my dialog boxes, instead of in the command or terminal window (between
running on Solaris, Linux, OSX and Windows systems there might not be any
command window or terminal window to show the traceback messages in). Do I
want to do something like override the print_exc (or format_exc?) method of
traceback to get the text of the message and call my dialog box routine? If
that is right how do I do that (monkeying with classes is all still a grey
area to me)?
You can overwrite the sys.exepthook() with your own function:
import sys
from traceback import format_exceptio n

def my_excepthook(e xctype, value, traceback):
details = "".join(format_ exception(excty pe, value, traceback))
# now show the details in your dialog box

sys.excepthook = my_excepthook
See the documentation for details:
http://docs.python.org/lib/module-sys.html#l2h-5125

Hope this helps,
Ziga

Oct 31 '06 #4
I usually have a function like this:

def get_excinfo_str ():
"""return exception stack trace as a string"""

(exc_type, exc_value, exc_traceback) = sys.exc_info()
formatted_excin fo = traceback.forma t_exception(exc _type, exc_value,
exc_traceback)
excinfo_str = "".join(formatt ed_excinfo)

del exc_type
del exc_value
del exc_traceback

return(excinfo_ str)
I can then call it from within "except" and print it to a log file.

Raghu.

Bob Greschke wrote:
I want to cause any traceback output from my applications to show up in one
of my dialog boxes, instead of in the command or terminal window (between
running on Solaris, Linux, OSX and Windows systems there might not be any
command window or terminal window to show the traceback messages in). Do I
want to do something like override the print_exc (or format_exc?) method of
traceback to get the text of the message and call my dialog box routine? If
that is right how do I do that (monkeying with classes is all still a grey
area to me)?

I kind of understand using the traceback module to alter the output of
exceptions that I am looking for, but I'm after those pesky ones that should
never happen. :)

Thanks!

Bob
Oct 31 '06 #5
dr*******@gmail .com wrote:
I usually have a function like this:

def get_excinfo_str ():
"""return exception stack trace as a string"""
(exc_type, exc_value, exc_traceback) = sys.exc_info()
The parens here can be skipped:
exc_type, exc_value, exc_traceback = sys.exc_info()
formatted_excin fo = traceback.forma t_exception(exc _type, exc_value,
exc_traceback)
excinfo_str = "".join(formatt ed_excinfo)
del exc_type
del exc_value
del exc_traceback
The three del lines above don't do anything (the return decrefs the locals).
return(excinfo_ str)
The parens here can be skipped as well:
return excinfo_str
--
--Scott David Daniels
sc***********@a cm.org
Nov 1 '06 #6

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

Similar topics

1
2090
by: fowlertrainer | last post by:
Hello python-list, This is a thread's run method. def run(self): try: page=urllib.urlopen(base) src=page.read() page=None self.ResultCode=0
1
2026
by: Oliver Walczak | last post by:
This seems to be a quite difficult approach. Try this: ##################################################################### import traceback class MyTraceback: def __init__(self): self.clear() def clear(self): self.s = '' def write(self, s):
0
1053
by: Ehab Teima | last post by:
Hello, I have built a server application. It's multithreaded, and everything works fine so far. I have only one issue with catching exceptions after I read the documentation but I can't find any problems yet. I'm catching all errors throughout the code into a function and I write the traceback to the server log as well as the screen. That's...
1
2196
by: Michael P. Soulier | last post by:
Hello, For a GUI app I've tried resetting sys.excepthook to my own exceptionhandler bound method, which accepts a type, value and traceback object. Now, the traceback module has print_exc and format_exc methods that are supposed to take a traceback object and return a formatted traceback like the default output. Unfortunately I keep...
2
2095
by: Philippe Martin | last post by:
Hi, Is there a way to catch traceback.print_exc() output into a string ? Philippe
4
2000
by: billiejoex | last post by:
Hi there, I'm facing a case where I need to get the traceback outptut when occurring an exception. I solved such problem by using traceback module in conjunction with StringIO: import StringIO, traceback try: raise Exception except:
8
1989
by: gregpinero | last post by:
I'm running code via the "exec in context" statement within a much larger program. What I would like to do is capture any possible errors and show a pretty traceback just like the Python interactive interpreter does, but only show the part of the traceback relating to the code sent to exec. For example here is the code I'm using: try:...
2
1954
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
1347
by: Gabriel Genellina | last post by:
En Mon, 26 May 2008 05:31:27 -0300, <Dominique.Holzwarth@ch.delarue.comescribió: Don't inherit from Exception - you should be able to log *any* exception, not only this specific one, I presume? print_exc writes "the exception currently being handled", not the one you're creating right now. Put the code above into your exception handler: ...
0
7524
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...
0
7960
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...
1
7475
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...
0
6048
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5372
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...
0
5089
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3501
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3483
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1061
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.