Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #5251

Re: Proper way to handle errors in a module

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!selfless.tophat.at!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'subject:module': 0.04; 'passes': 0.05; 'string,': 0.05; 'written.': 0.07; 'python': 0.07; ':-)': 0.07; 'from:addr:python': 0.09; 'pm,': 0.11; 'this:': 0.11; 'exception': 0.12; 'def': 0.13; 'wrote:': 0.14; '42,': 0.16; 'clause,': 0.16; 'docs.': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 0.16; 'keyerror:': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'received:84.92': 0.16; 'received:84.92.122': 0.16; 'received:84.92.122.60': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'reply-to:addr :python-list': 0.16; 'roy': 0.16; 'invalid': 0.16; 'code': 0.22; 'header:In-Reply-To:1': 0.22; 'somebody': 0.25; 'received:84': 0.25; 'function': 0.27; 'raise': 0.29; "can't": 0.31; 'to:addr :python-list': 0.32; 'got': 0.34; 'there': 0.35; 'header:User- Agent:1': 0.35; 'like:': 0.35; 'reply-to:addr:python.org': 0.35; 'trigger': 0.35; 'try:': 0.35; 'doing': 0.36; 'setting': 0.36; 'andrew': 0.38; 'so,': 0.38; 'help': 0.39; 'to:addr:python.org': 0.39; 'except': 0.39; 'reply-to:no real name:2**0': 0.72; 'header :Reply-To:1': 0.72; 'job?': 0.84; 'subject:handle': 0.84
X-IronPort-Anti-Spam-Filtered true
X-IronPort-Anti-Spam-Result AlEHALAzzE1UXebj/2dsb2JhbACYAY14d8k9hhUElCOKQg
Date Thu, 12 May 2011 20:25:21 +0100
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.17) Gecko/20110414 Thunderbird/3.1.10
MIME-Version 1.0
To python-list@python.org
Subject Re: Proper way to handle errors in a module
References <mailman.1415.1305134998.9059.python-list@python.org> <roy-FB35E1.14050911052011@news.panix.com> <4DCC318E.7020809@gmail.com>
In-Reply-To <4DCC318E.7020809@gmail.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
Reply-To python-list@python.org
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1485.1305228330.9059.python-list@python.org> (permalink)
Lines 27
NNTP-Posting-Host 82.94.164.166
X-Trace 1305228330 news.xs4all.nl 41114 [::ffff:82.94.164.166]:41222
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:5251

Show key headers only | View raw


On 12/05/2011 20:14, Andrew Berg wrote:
> 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.
 >
You can raise an exception wherever you like! :-)

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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