Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #52929
| From | Dave Angel <davea@davea.name> |
|---|---|
| Subject | Re: Exception Handling Practices / Patterns |
| Date | 2013-08-24 07:18 +0000 |
| References | <6446b59c-ff06-4c1b-8c54-79bcb1140be5@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.189.1377328762.19984.python-list@python.org> (permalink) |
snarf wrote: > Greetings, > > As I tread through my journey of OO I am trying to determine if there is a good approach for exception handling within classes. > > From my readings and gatherings - it seems I have found a common theme, but I am trying to solicit from the experts. > > Here is what I have found (I may be restating the obvious so please forgive me in advance): > > * Seems like exception handing within Classes is largely avoided and is typically only used when calling external libraries. > * Try/Except type statements seem to be used more within modules, main functions, wrapper scripts. Exceptions are used when useful. I don't see any bias towards any one location. > * Classes should be coded in a way that exceptions You seem to be missing the last part of this sentence. > * Better to never write your own exceptions (unless you absolutely have to). If you mean to avoid writing exception classes, then I say nonsense. Just derive them from the closest meaningful exception class, so that a user can combine handlers when reasonable. > * Using Exception is typically a bad. More specific the better. If you mean in an except statement, then I'd agree. > * Exceptions should never fail silently. (Should exceptions always be logged?) Exceptions should be caught if you can handle them, or if you need to convert them to a different exception that someone further up the stack can handle. Sometimes handling means do nothing. > > Best site I have found for exceptions (hopefully this helps someone): > * http://c2.com/cgi/wiki?ExceptionPatterns But that's for Java. java is not C++, and neither is it Python. For one thing, Python exception overhead is deliberately much less, and they are used more freely. Notice that exceptions are used to terminate for loops, and that's a *normal* exit to the loop. They also appear in other places under the covers. Don't be afraid of them. -- DaveA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Exception Handling Practices / Patterns snarf <frank.ruiz@gmail.com> - 2013-08-23 22:25 -0700
Re: Exception Handling Practices / Patterns Dave Angel <davea@davea.name> - 2013-08-24 07:18 +0000
Re: Exception Handling Practices / Patterns frank.ruiz@gmail.com - 2013-08-24 16:30 -0700
Re: Exception Handling Practices / Patterns Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-24 10:27 +0000
Re: Exception Handling Practices / Patterns MRAB <python@mrabarnett.plus.com> - 2013-08-24 17:32 +0100
Re: Exception Handling Practices / Patterns Terry Reedy <tjreedy@udel.edu> - 2013-08-24 15:57 -0400
Re: Exception Handling Practices / Patterns Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-24 23:11 +0000
Re: Exception Handling Practices / Patterns frank.ruiz@gmail.com - 2013-08-24 16:32 -0700
csiph-web