views:

438

answers:

3

I have a wxPython application that relies on an external config file. I want provide friendly message dialogs that show up if there are any config errors. I've tried to make this work by wrapping my app.MainLoop() call in a try/except statement.

The code below works for the init code in my MainWindow frame class, but doesn't catch any exceptions that occur within the MainLoop. How can I catch these exceptions as well?

if __name__ == '__main__':
    app = MyApp(0)
    try:
        MainWindow(None, -1, 'My Cool App')
        app.MainLoop()
    except ConfigParser.Error, error_message:
        messagebox = wx.MessageDialog(None, error_message, 'Configuration Error', wx.OK | wx.ICON_ERROR)
        messagebox.ShowModal()

I've read some mention of an OnExceptionInMainLoop method that can be overridden in the wx.App class, but the source I read must be out of date (2004) since wx.App no longer seems to have a method by that name.

EDIT:

I need to be able to catch unhandled exceptions during my mainloop so that I can further handle them and display them in error dialogs, not pass silently, and not terminate the app.

The sys.excepthook solution is too low level and doesn't play nice with the wxPython mainloop thread. While the link to the other answer does the same try/except wrapping around the mainloop which doesn't work due, once again, to wxPython spawning a different thread for the app/ui.

A: 

Don't know if this will work for a wxPython application, but in the sys module you can overwrite the excepthook attribute, which is a function called with 3 arguments, (type, value, traceback), when an uncaugth exception is caught. You can install your own function in there that handles only the exceptions you want, and call the original function for all the others.

Consult: http://docs.python.org/library/sys.html#sys.excepthook

A: 

Perhaps this question might be of some use, it tries to capture all exceptions.

SteveL
Notice that the example is wrapping the app.MainLoop() in a try/except. This doesn't work, at least not in any of my attempts.
Soviut
+1  A: 

I coded something like this for Chandler, where any unhandled exceptions pop up a window with the stack and other info, and users can put in additional comments (what did they do when it happened etc.) and submit it for Chandler developers. A bit like the Mozilla Talkback (nowadays they use Breakpad I believe) feature if you will.

To do this in wxPython, you need to provide redirect parameter to wx.App. This will pop up wx.PyOnDemandOutputWindow (you will probably want to override it to provide a nicer looking implementation).

The relevant source files in Chandler are here:

  • Chandler.py starts the application and sets the redirect attribute, as well as tries to catch and display error dialogs in case normal application startup fails
  • Application.py customizes the application object, including setting up our customized wx.PyOnDemandOutputWindow
  • feedback.py has the implementation for the customized wx.PyOnDemandOutputWindow; it will additionally need feedback.xrc and feedback_xrc.py
Heikki Toivonen