How can I catch all exceptions from a wxPython application?

I am writing a small debugging application for a small set that we are developing, and I would like to deploy it to several users to find out if they can cause any crashes. Does anyone know a way to efficiently package a wxPython application to catch all unhandled exceptions that could lead to an application crash?

Ideally, I would like to capture the entire output (and not just the errors) and write it to a file. Any unhandled exceptions should be logged in the current file and then allow the exception as usual (i.e. the logging process should be transparent).

I am sure that someone must have done something in this direction before, but I was not able to show anything useful using google.

+6
python exception error-handling wxwidgets
source share
4 answers

To register standard output, you can use a stdout wrapper like this one:

from __future__ import with_statement class OutWrapper(object): def __init__(self, realOutput, logFileName): self._realOutput = realOutput self._logFileName = logFileName def _log(self, text): with open(self._logFileName, 'a') as logFile: logFile.write(text) def write(self, text): self._log(text) self._realOutput.write(text) 

Then you should initialize it in your main Python file (the one that runs everything):

 import sys sys.stdout = OutWrapper(sys.stdout, r'c:\temp\log.txt') 

As for exception logging, the easiest way is to wrap MainLoop wx.App method in try..except, and then extract the exception information, save it somehow, and then re-create the exception via raise , for example:

 try: app.MainLoop() except: exc_info = sys.exc_info() saveExcInfo(exc_info) # this method you have to write yourself raise 
+6
source share

To handle exceptions if your log file is open as a log:

 import sys import traceback def excepthook(type, value, tb): message = 'Uncaught exception:\n' message += ''.join(traceback.format_exception(type, value, tb)) log.write(message) sys.excepthook = excepthook 
+9
source share

you can use

sys.excepthook

(see Python docs )

and assign for it some user-defined object that will catch all exceptions that were not previously detected in your code. Then you can write any message to any file that you want, along with tracing and do anything with exception (reraise, display an error message and allow the user to continue using your application, etc.).

Regarding stdout registration, the best way for me was to write something similar to DzinX OutWrapper.

If you are in the debugging phase, consider clearing the log files after each entry. This is bad for performance, but if you manage to invoke segfault in some basic C code, your logs will not mislead you.

+3
source share

There are different ways. However, you can put a try..catch block in wxApplication :: OnInit, which will not always work with Gtk.

A good alternative would be to override Application :: HandleEvent in your wxApplication derived class and write this code:

 void Application::HandleEvent(wxEvtHandler* handler, wxEventFunction func, wxEvent& event) const { try { wxAppConsole::HandleEvent(handler, func, event); } catch (const std::exception& e) { wxMessageBox(std2wx(e.what()), _("Unhandled Error"), wxOK | wxICON_ERROR, wxGetTopLevelParent(wxGetActiveWindow())); } } 

This is a C ++ example, but you can safely translate it to Python.

+1
source share

All Articles