Overriding traceback print_exc()?

Ziga Seilnacht ziga.seilnacht at gmail.com
Tue Oct 31 21:43:58 CET 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?  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_exception

def my_excepthook(exctype, value, traceback):
    details = "".join(format_exception(exctype, value, traceback))
    # now show the details in your dialog box

sys.excepthook = my_excepthook

See the documentation for details:

Hope this helps,

More information about the Python-list mailing list