Error handling in wxPython

Monday 24 March 2003

I wrote my first useful wxPython application over the weekend, and here’s my number one helpful tip for those of you out there who are trying to get started with wxPython.

Maybe there’s something about my installation that isn’t right, but when there was an error in my code, I saw a little stderr window pop up, and then disappear before I could read what it said. Very frustrating. Here’s what I finally figured out to fix it.

Instead of the usual main loop:

app = MyApp()
app.MainLoop()

Use this instead:

try:
    app = MyApp()
    app.MainLoop()
except:
    import sys, traceback
    xc = traceback.format_exception(*sys.exc_info())
    wxMessageBox(''.join(xc))

Now errors in executing the main loop will be displayed in a message box, where (imagine this) you can read them!

Comments

[gravatar]
Anon 9:44 PM on 1 Nov 2008

Hah! Thanks! 5 five years later, I suffer from the same problem and the cure still works.

[gravatar]
anon2 12:10 AM on 22 Jan 2013

10 YEARS later and this is the best wx troubleshooting i got!

Add a comment:

Ignore this:
Leave this empty:
Name is required. Either email or web are required. Email won't be displayed and I won't spam you. Your web site won't be indexed by search engines.
Don't put anything here:
Leave this empty:
URLs auto-link and some tags are allowed: <a><b><i><p><br><pre>.