[PyKDE] problem with exceptions within events

Giovanni Bajo rasky at develer.com
Thu Feb 24 00:44:09 GMT 2005

Truls A. Tangstad <kerfue+pykde at herocamp.org> wrote:

>> it seems that Python exceptions generated within event handlers do
>> not get propagated correctly. We use a sys.excepthook to abort
>> gracefully with a messagebox from our application, and this seems to
>> work in most cases but not within event handlers.
> I think this is by design as most event-triggers are not prepared to
> handle an exception caused by an event.
> I had the same problem, but after thinking about it for a while i
> realized that having events propagate exceptions to the initiator
> would at most times be problematic and probably cause more problems
> then what would be gained by what you are trying to achieve.
> Events are often sent from other threads and end up in a thread-queue
> in the main thread (event/gui-thread). The gui loops through this
> thread and sends events where needed. If the handler of an event
> throws an exception, there's now nowhere for it to go, except causing
> the event-loop to crash... which Qt (PyQt?) solves by instead printing
> the exception and then ditching it.
> I'm sure someone will correct me if I'm off the mark here...

I reckon you have slightly misunderstood my problem.

Usually, when an exception is raised from Python code using PyQt, the
traceback is printed to standard output and execution of the program
continues. I believe this is done by PyQt, but I would appreciate if Phil
explained this.

The problem is: when the exception is raised from within the overriden
method of an event handler (as opposed to an exception raised from within
the constructor of a subclass, or from within a slot called by a signal),
the exception is *totally* ignored. There is no acknowledgement
whatsoever. Nothing. Nada. I have no way to know that my code did not

Moreover, I will add that the behaviour of just printing the exception on
the console and ignore it is inacceptable in our application. We have
fixed this by overwriting sys.excepthook with a special function which
logs the traceback to a file, displays a message box saying something like
"internal error", and finally aborts the application. I would expect the
exception raised by the event handler to call our custom excepthook just
like other exceptions raised in different contexts, but it would be fine
if it *just* printed something on the console. At least, I would know
there is a problem.

Giovanni Bajo

More information about the PyQt mailing list