Overriding traceback print_exc()?
Fredrik Lundh
fredrik at pythonware.com
Tue Oct 31 14:08:10 EST 2006
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("oops!")
try:
main()
except (KeyboardInterrupt, 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_exception(
sys.last_type, sys.last_value, sys.last_traceback
))
atexit.register(postmortem)
def main():
raise RuntimeError("oops!")
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>
More information about the Python-list
mailing list