Cut MainLoop exceptions and display them in MessageDialogs

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

The code below works for the initialization code in my MainWindow class frame, but it doesn't catch any exceptions that happen in MainLoop. How can I catch these exceptions?

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 read some mention of the OnExceptionInMainLoop method that can be overridden in the wx.App class, but the source I read should be deprecated (2004) since wx.App no ​​longer has a method to name it.

EDIT:

I need to catch unhandled exceptions during my mainloop so that I can handle them and display them in error dialogs, not silently skip and not interrupt the application.

Sys.excepthook solution is too low and does not work well with wxPython mainloop thread. While the link to another answer does the exact same attempt / excluding the wrapper around the mainloop which does not work as expected, once again for wxPython to create another thread for the app / ui.

+1


source to share


4 answers


I have coded something like this for Chandler where any unhandled exceptions will cause a window with a stack and other information and users can add additional comments (what they did when it happened, etc.) and send it to the Chandler developers. A bit like the Mozilla the Talkback ( currently they use Breakpad, I believe), if you do this.

To do this, in wxPython you need to specify a redirect parameter to wx.App. The message wx.PyOnDemandOutputWindow will appear (you probably want to override this to provide a more realistic implementation).



The corresponding source files in Chandler are located here:

  • Chandler.py launches the application and sets the redirect attribute, and also tries to catch and display error dialogs in case of normal application launch fails
  • Application.py configures the application object including setting the configured wx.PyOnDemandOutputWindow
  • feedback.py has an implementation for the configured wx.PyOnDemandOutputWindow; it will also need feedback .xrc and feedback_xrc.py
+1


source


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 with three arguments (type, value, traceback)

, when an uncaugth exception is caught. You can set your own function in there that only handles the exceptions you want and call the original function for everyone else.



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

+2


source


Maybe this question can be helpful, it tries to capture all exceptions.

0


source


Posting a solution that worked for me with a very similar problem.

import wx
import sys
import traceback

class Frame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None)
        panel = wx.Panel(self)
        m_close = wx.Button(panel, -1, "Error")
        m_close.Bind(wx.EVT_BUTTON, self.OnErr)
    def OnErr(self, event):
        1/0

def handleGUIException(exc_type, exc_value, exc_traceback):
    err_msg = ''.join(traceback.format_exception(exc_type, exc_value, exc_traceback))
    err_msg += '\n Your App will now terminate'
    # Here collecting traceback and some log files to be sent for debugging.
    # But also possible to handle the error and continue working.
    dlg = wx.MessageDialog(None, err_msg, 'Termination dialog', wx.OK | wx.ICON_ERROR)
    dlg.ShowModal()
    dlg.Destroy()
    sys.exit()

sys.excepthook = handleGUIException

if __name__ == '__main__':
    app = wx.App(redirect=False) 
    top = Frame()
    top.Show()
    app.MainLoop()

      

0


source







All Articles