<br><br>---------- Forwarded message ----------<br><span class="gmail_quote">From: <b class="gmail_sendername">Arve Knudsen</b> <<a href="mailto:arve.knudsen@gmail.com">arve.knudsen@gmail.com</a>><br>Date: May 23, 2007 3:29 PM
<br>Subject: Re: [PyQt] Exception handling hook<br>To: Sundance <<a href="mailto:sundance@ierne.eu.org">sundance@ierne.eu.org</a>><br><br></span><span class="q"><span class="gmail_quote">On 5/23/07, <b class="gmail_sendername">
Sundance</b> <<a href="mailto:sundance@ierne.eu.org" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">sundance@ierne.eu.org</a>> wrote:</span><blockquote class="gmail_quote" style="margin-top: 0; margin-right: 0; margin-bottom: 0; margin-left: 0; margin-left: 0.80ex; border-left-color: #cccccc; border-left-width: 1px; border-left-style: solid; padding-left: 1ex">
Arve Knudsen wrote:<br><br>> Is it possible to register a hook with QApplication in order to be<br>> notified of an unhandled exception?<br><br>Hi Arve,<br><br>Exceptions live in the Python realm, so they have nothing to do with
<br>QApplication as far as I know.</blockquote></span><div><br>It was hoping for some sort of mechanism to handle exceptions once they propagate to the Qt event loop (managed by QApplication).<br></div><span class="q"><br>
<blockquote class="gmail_quote" style="margin-top: 0; margin-right: 0; margin-bottom: 0; margin-left: 0; margin-left: 0.80ex; border-left-color: #cccccc; border-left-width: 1px; border-left-style: solid; padding-left: 1ex">
You can install an exception handler with sys.excepthook.<br><br>Here's a PyQt3 example (which might also work with PyQt4, I haven't<br>tested):<br><br><br>def handle_exception(exc_type, exc_value, exc_traceback):
<br><br> filename, line, dummy, dummy = \<br> traceback.extract_tb(exc_traceback).pop()<br> filename = os.path.basename(filename)<br> error = "%s: %s" % (str(exc_type).split(".")[-1], exc_value)<br>
<br> QMessageBox.critical(None, "ERROR",<br> "There has been an error: "<br> + "<b>%s</b> " % error<br> + "on line %d, file %s" % (line, filename))<br><br><br>
You can (and probably should) close your application then.<br><br>You can install this handler with:<br>sys.excepthook=handle_exception<br><br>(Since the handler uses QMessageBox, install it AFTER instanciating your<br>QApplication.)
</blockquote></span><div><br>This is about how I've handled it so far in my own application. But I found out the other day that this approach is not sufficient in all cases unfortunately. In one part of my application, invoking a certain Qt method (don't remember the exact situation at the moment) may cause a Python callback to be invoked in return. So there are then the following layers: Qt -> Python -> Qt -> Python. If an exception is raised in the last Python layer, it is swallowed by the excepthook and a nice message box pops up, BUT once control returns to the first Python layer (invoked by the event loop) the flow is as if no exception has occurred (since the exception was already handled).
</div><br>Therefore, the solution as far as I can tell is for PyQt to trap exceptions in the outermost layer and allow client code to hook into this.<br><span class="sg"><br>Arve<br></span>