<br><br>---------- Forwarded message ----------<br><span class="gmail_quote">From: <b class="gmail_sendername">Arve Knudsen</b> &lt;<a href="mailto:arve.knudsen@gmail.com">arve.knudsen@gmail.com</a>&gt;<br>Date: May 23, 2007 3:29 PM
<br>Subject: Re: [PyQt] Exception handling hook<br>To: Sundance &lt;<a href="mailto:sundance@ierne.eu.org">sundance@ierne.eu.org</a>&gt;<br><br></span><span class="q"><span class="gmail_quote">On 5/23/07, <b class="gmail_sendername">
Sundance</b> &lt;<a href="mailto:sundance@ierne.eu.org" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">sundance@ierne.eu.org</a>&gt; 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>&gt; Is it possible to register a hook with QApplication in order to be<br>&gt; 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&#39;s a PyQt3 example (which might also work with PyQt4, I haven&#39;t<br>tested):<br><br><br>def handle_exception(exc_type, exc_value, exc_traceback): 
<br><br>&nbsp;&nbsp;filename, line, dummy, dummy = \<br>&nbsp;&nbsp;&nbsp;&nbsp;traceback.extract_tb(exc_traceback).pop()<br>&nbsp;&nbsp;filename = os.path.basename(filename)<br>&nbsp;&nbsp;error = &quot;%s: %s&quot; % (str(exc_type).split(&quot;.&quot;)[-1], exc_value)<br>
<br>&nbsp;&nbsp;QMessageBox.critical(None, &quot;ERROR&quot;,<br>&nbsp;&nbsp;&nbsp;&nbsp;&quot;There has been an error: &quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;+ &quot;&lt;b&gt;%s&lt;/b&gt; &quot; % error<br>&nbsp;&nbsp;&nbsp;&nbsp;+ &quot;on line %d, file %s&quot; % (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&nbsp;is&nbsp;about&nbsp;how&nbsp;I&#39;ve&nbsp;handled&nbsp;it&nbsp;so&nbsp;far&nbsp;in&nbsp;my&nbsp;own&nbsp;application.&nbsp;But&nbsp;I&nbsp;found&nbsp;out&nbsp;the&nbsp;other&nbsp;day&nbsp;that this&nbsp;approach is&nbsp;not&nbsp;sufficient in all cases unfortunately. In one part of my application, invoking a certain Qt method (don&#39;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 -&gt; Python -&gt; Qt -&gt; 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, &nbsp;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>