[wxPython-users] Advice on catching exceptions?
Peter Damoc
pdamoc at gmail.com
Wed Jun 7 04:03:06 PDT 2006
On 6/7/06, Marc Hedlund <marc at precipice.org> wrote:
>
> ...
> My main loop used to look like this:
>
> if __name__ =3D=3D '__main__':
> try:
> # Main event loop startup.
> app =3D MyApp(redirect=3DFalse)
> app.MainLoop()
> except Exception, detail:
> if not isinstance(detail, SystemExit):
> logger =3D logging.getLogger("myloggername")
> logger.critical("Last-gasp error handler caught an
> exception!\n%s",
> exc_info=3Dsys.exc_info())
> ...
> Thanks for any suggestions or advice.
>
> Regards,
> Marc
>
My advice is to Relax and... don't do it.
This kind of code tells me that you're not paying enough attention to where
errors can appear. I suggest that before the code grows any larger try to
split it into code that can break and code that is relatively safe.
>From my experience wxPython code, if treated right, is rather solid... the
problems usually are found in custom computation code or in improper user
input handling... Split your code in a MVC or MVP architecture, use
decorators to add exception handling to problem event handlers or better
yet, delegate the event handling to a presenter method that will split the
handling in more digestible bits of code.
One suggestion that I found useful and I keep reminding it to myself...
write as much code as possible, fight the urge to write less code... so for
example if you have a method that is one page large and a little gremlin in
the head is saying "forget about spliting it in smaller methods, together
with comments and whitespace will be twice as large", well, I actively try
to shut that gremlin up and write one more page of code (split the method,
add comments, add logging) ;)
-- =
Peter Damoc
Lead developer for
Inamed CAIS Planning System
http://www.cms.ro/tech.php?loc=3Dcais&lang=3Den
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.wxwidgets.org/pipermail/wxpython-users/attachments/200606=
07/04d95bed/attachment.htm
More information about the wxpython-users
mailing list