[PyQt] Catching exceptions everywhere

Igor Prischepoff igor at tyumbit.ru
Fri May 8 06:29:36 BST 2009


Hello,
How about that overriding 
sys.excepthook ?
This code was shamelessly stolen and modified from  
Eric IDE ( credits goes to author )

import blablabla....

sys.excepthook = excepthook

def excepthook(excType, excValue, tracebackobj):
    """
    Global function to catch unhandled exceptions.
    
    @param excType exception type
    @param excValue exception value
    @param tracebackobj traceback object
    """
    separator = '-' * 80
    logFile = "simple.log"
    notice = \
        """An unhandled exception occurred. Please report the problem\n"""\
        """using the error reporting dialog or via email to <%s>.\n"""\
        """A log has been written to "%s".\n\nError information:\n""" % \
        ("yourmail at server.com", "")
    versionInfo="0.0.1"
    timeString = time.strftime("%Y-%m-%d, %H:%M:%S")
    
    
    tbinfofile = cStringIO.StringIO()
    traceback.print_tb(tracebackobj, None, tbinfofile)
    tbinfofile.seek(0)
    tbinfo = tbinfofile.read()
    errmsg = '%s: \n%s' % (str(excType), str(excValue))
    sections = [separator, timeString, separator, errmsg, separator, tbinfo]
    msg = '\n'.join(sections)
    try:
        f = open(logFile, "w")
        f.write(msg)
        f.write(versionInfo)
        f.close()
    except IOError:
        pass
    errorbox = QtGui.QMessageBox()
    errorbox.setText(str(notice)+str(msg)+str(versionInfo))
    errorbox.exec_()
    

Put it somewhere in your main.py and you got nice messagebox when exception
occurs anywhere in your code.

---
igor at tyumbit.ru

-----Original Message-----
From: pyqt-bounces at riverbankcomputing.com
[mailto:pyqt-bounces at riverbankcomputing.com] On Behalf Of Lukas Hetzenecker
Sent: Thursday, May 07, 2009 9:32 PM
To: pyqt at riverbankcomputing.com
Cc: Jeremy Sanders
Subject: Re: [PyQt] Catching exceptions everywhere

Hello,

you could redirect the interpreter's standard error output stream:
sys.stderr = YourClass(window.edit,  sys.__stderr__,  QColor(Qt.red))
YourClass in an "file-like" object (it has an write function) window.edit is
an instance of Qt's QTextEdit class and is used to display the message.

Here is an example: http://series60-remote.svn.sf.net/viewvc/series60-
remote/trunk/pc/series60-remote.py?view=markup ,  line 288

The class QtOutput is defined here: http://series60-
remote.svn.sf.net/viewvc/series60-
remote/trunk/pc/lib/log.py?revision=257&view=markup , line 36

And the window that actually displays an error message is here: 
http://series60-remote.svn.sf.net/viewvc/series60-
remote/trunk/pc/window/log.py?view=markup

A screenshot is here:
http://imagebin.ca/view/5rSa--2X.html
The Traceback is colored red in the window.

Starting with this is should be easy to get a "report bug"-dialog ;-)

If you have any questions please just ask.

Greetings,
Lukas

Am Donnerstag 07 Mai 2009 13:17:54 schrieb Jeremy Sanders:
> Is there a way to catch Python exceptions in all parts of a PyQt program?
>
> I have a "report bug" dialog box which gets called if an exception 
> occurs in the most frequently used parts of my program, but it would 
> be nice if I could catch every unhandled exception globally.
>
> The really nice thing about python is that program tends to keep 
> running despite an exception in a slot, so we don't get immediate data
loss.
>
> Thanks
>
> Jeremy


_______________________________________________
PyQt mailing list    PyQt at riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt



More information about the PyQt mailing list