[wxPython-users] Advice on catching exceptions?
Robin Dunn
robin at alldunn.com
Wed Jun 7 15:13:44 PDT 2006
Marc Hedlund wrote:
>
> Hi,
>
> I have a wxPython app which makes extensive use of the python logging
> package to write out a log file. For the most part, that works great,
> but in a few cases, exceptions seem to elude the exception handlers I
> have set up: (1) in some event handlers that don't have enough exception
> handling themselves; and (2) when the logging package itself throws an
> exception. In these cases, on Windows 2000, a dialog appears when the
> application quits, saying "Errors occurred, see C:\Program
> Files\<mydir>\<myexe>.exe.txt for more information" (paraphrased).
>
> My main loop used to look like this:
>
> if __name__ == '__main__':
> try:
> # Main event loop startup.
> app = MyApp(redirect=False)
> app.MainLoop()
> except Exception, detail:
> if not isinstance(detail, SystemExit):
> logger = logging.getLogger("myloggername")
> logger.critical("Last-gasp error handler caught an
> exception!\n%s",
> exc_info=sys.exc_info())
>
> That usually worked, but like I say, some event handler exceptions
> seemed to elude the except clause.
See:
http://lists.wxwidgets.org/cgi-bin/ezmlm-cgi?11:msp:48315:oadlcbijoffnbmmeppbo
> I just made one change to my code in
> order to try to prevent this:
>
> app = MyApp(redirect=True)
>
> Later on, after I've used wx.StandardPaths to find a good place for the
> logfile, I call the following inside my app class:
>
> self.RedirectStdio(filename=logpath)
>
> Although, redirecting output doesn't seem like it would make the except
> clause any more aggressive or effective -- just that it would cause the
> App object to do its best to catch stdout/stderr. What I really want is
> a more effective except clause.
>
> I don't have a good reproduction case for the "errors occurred" problem
> I was seeing before, so I'm wondering if there's anything else I should
> do in order to catch as many exceptions as possible. Is using
> wx.App.OnUnhandledException, wx.App.OnFatalException, or
> wx.App.OnExceptionInMainLoop advisable? None of those seem to be
> documented in the python docs, and I'm not sure whether they're needed
> or useful in this case.
They are currently not wrapped for Python, and they are for dealing with
C++ exceptions, not Python exceptions. Also, IIRC, they are different
based on whether it is a debug or a release build.
--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!
More information about the wxpython-users
mailing list