[PyQt] Catching python exceptions

Joseph Rawson umeboshi at gregscomputerservice.com
Thu Mar 22 22:07:19 GMT 2007

On Thursday 22 March 2007 16:47, Luper Rouch wrote:
> Hi,
> What is the proper way to add a global exceptions catcher to a
> QApplication, for example to bring a bug report dialog when an exception
> reaches the main event loop ?
> I guess I have to make my own event loop but my attempts failed
> miserably, any pointers to an example ?
I just asked this question not long ago.  

You can set sys.excepthook to a function that creates a dialog window.


import traceback
from StringIO import StringIO

separator = '-' * 80

def excepthook(type, value, tracebackobj):
    tbinfofile = StringIO()
    traceback.print_tb(tracebackobj, None, tbinfofile)
    tbinfo = tbinfofile.read()
    errmsg = '%s: %s' % (str(type), str(value))
    sections = [separator, errmsg, separator]
    msg = '\n'.join(sections)
    KMessageBox.detailedError(None, msg, tbinfo)


and in the main script - I do the override just before creating
the application instance.

KCmdLineArgs.init(sys.argv, aboutData)
sys.excepthook = excepthook
#raise StandardError, 'testing an error'
# setup application
app = MainApplication()

you could uncomment the raise statement above to do test the excepthook out.

I just recently started using the detailedError box and it looks pretty nice.

I hope this helps. :)

> Thanks,
> Luper Rouch
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Joseph Rawson
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20070322/0632110e/attachment.bin

More information about the PyQt mailing list