On 2011.05.11 01:05 PM, Roy Smith wrote:
> 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)
Is there any way to do this without purposely setting up the code to
trigger an arbitrary exception if the function can't do its job? That
is, can I raise an UnknownBirdError outside of an except clause, and if
so, how? I can't find much help for doing this in Python 3, even in the
official docs.
--
http://mail.python.org/mailman/listinfo/python-list
- Proper way to handle errors in a module Andrew Berg
- Re: Proper way to handle errors in a module Patty
- Re: Proper way to handle errors in a module Andrew Berg
- RE: Proper way to handle errors in a module Prasad, Ramit
- Re: Proper way to handle errors in a module MRAB
- Re: Proper way to handle errors in a module Roy Smith
- Re: Proper way to handle errors in a module Andrew Berg
- Re: Proper way to handle errors in a module MRAB
- Re: Proper way to handle errors in a mod... Andrew Berg
- Re: Proper way to handle errors in ... Corey Richardson
- Re: Proper way to handle errors... Andrew Berg
- Re: Proper way to handle errors... Ben Finney
- Re: Proper way to handle errors in ... Tycho Andersen
- Re: Proper way to handle errors in ... MRAB
- Re: Proper way to handle errors in ... Ethan Furman
- Re: Proper way to handle errors in ... Steven D'Aprano
