Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #5139
| From | Roy Smith <roy@panix.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Proper way to handle errors in a module |
| Date | 2011-05-11 14:05 -0400 |
| Organization | PANIX Public Access Internet and UNIX, NYC |
| Message-ID | <roy-FB35E1.14050911052011@news.panix.com> (permalink) |
| References | <mailman.1415.1305134998.9059.python-list@python.org> |
In article <mailman.1415.1305134998.9059.python-list@python.org>,
Andrew Berg <bahamutzero8825@gmail.com> wrote:
> I'm a bit new to programming outside of shell scripts (and I'm no expert
> there), so I was wondering what is considered the best way to handle
> errors when writing a module. Do I just let exceptions go and raise
> custom exceptions for errors that don't trigger a standard one? Have the
> function/method return nothing or a default value and show an error
> message? I'm sure there's not a clear-cut answer, but I was just
> wondering what most developers would expect a module to do in certain
> situations.
In general, raise an exception when there is no valid value that can be
returned from a function. Sometimes it makes more sense to return 0,
None, an empty list, etc. So:
count_vowels("banana") ==> 3
count_vowels("xyzzy") ==> 0 # counting "y" as not a vowel
count_vowels("") ==> 0
count_vowels(None) ==> raises TypeError
You want to raise specific errors. Let's say you've got a function like
this:
def airspeed(swallow):
speeds = {"european": 42,
"african", 196}
return speeds[swallow]
If somebody passes an invalid string, it will raise KeyError as written.
Better to do something like:
def airspeed(swallow):
speeds = {"european": 42,
"african", 196}
try:
return speeds[swallow]
except KeyError:
raise UnknownBirdError(swallow)
This lets somebody catch UnknownBirdError at some higher level and do
something useful. If you let KeyError escape, that's a lot harder to
deal with because it could come from almost anywhere.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Proper way to handle errors in a module Andrew Berg <bahamutzero8825@gmail.com> - 2011-05-11 12:29 -0500
Re: Proper way to handle errors in a module Roy Smith <roy@panix.com> - 2011-05-11 14:05 -0400
Re: Proper way to handle errors in a module Andrew Berg <bahamutzero8825@gmail.com> - 2011-05-12 14:14 -0500
Re: Proper way to handle errors in a module MRAB <python@mrabarnett.plus.com> - 2011-05-12 20:25 +0100
Re: Proper way to handle errors in a module Andrew Berg <bahamutzero8825@gmail.com> - 2011-05-12 15:12 -0500
Re: Proper way to handle errors in a module Corey Richardson <kb1pkl@aim.com> - 2011-05-12 16:20 -0400
Re: Proper way to handle errors in a module Tycho Andersen <tycho@tycho.ws> - 2011-05-12 15:26 -0500
Re: Proper way to handle errors in a module Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-13 00:43 +0000
Re: Proper way to handle errors in a module MRAB <python@mrabarnett.plus.com> - 2011-05-12 21:26 +0100
Re: Proper way to handle errors in a module Ethan Furman <ethan@stoneleaf.us> - 2011-05-12 13:40 -0700
Re: Proper way to handle errors in a module Andrew Berg <bahamutzero8825@gmail.com> - 2011-05-12 15:35 -0500
Re: Proper way to handle errors in a module Ben Finney <ben+python@benfinney.id.au> - 2011-05-13 10:56 +1000
csiph-web