[wxPython-users] Better exception handling

Riaan Booysen riaan at e.co.za
Sat Jun 30 06:39:27 PDT 2007


Hi Jorgen,

Jorgen Bodde wrote:
> Hi All,
> 
> I love wxPython! One thing that worries me for my end-users is the
> inability to see potential errors. When my GUI app is ran from a
> console window, the exception is thrown in there. But when somethign
> happens and the console window is not there, I would like to see an
> error window similar to the stack walker. The way it is now, errors
> and exceptions are eaten if I rename my app's extension to myapp.pyw
> ..
> 
> I would not like that as it leads to undesired behaviour and people
> not seeing an error and think a particular button does not do anything
> can cause more harm then showing an error dialog. Also if there is no
> feedback, not many people will file a report.
> 
> Is there a way to get errors back from python's exceptions when there
> is no console window??
> 
> Regards,
> - Jorgen

Here is a simple pattern I use for gui apps with wxPython/excepthook:

import sys, traceback, wx

def excepthook(type, value, trace):
     if wx and sys and traceback:
         exc = traceback.format_exception(type, value, trace)
         for e in exc: wx.LogError(e)
         wx.LogError('Unhandled Error: %s: %s'%(str(type), str(value)))
         sys.__excepthook__(type, value, trace)

And in your initialization code:
     sys.excepthook = excepthook





More information about the wxpython-users mailing list