Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!xlned.com!feeder7.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 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; 'pm,': 0.11; 'this:': 0.11; 'exception': 0.12; 'def': 0.13; 'wrote:': 0.14; '42,': 0.16; 'clause,': 0.16; 'docs.': 0.16; 'keyerror:': 0.16; 'received:192.168.1.6': 0.16; 'roy': 0.16; 'invalid': 0.16; 'code': 0.22; 'header:In-Reply-To:1': 0.22; 'received:209.85.214.174': 0.23; 'received:mail- iw0-f174.google.com': 0.23; 'somebody': 0.25; 'function': 0.27; 'raise': 0.29; 'message-id:@gmail.com': 0.30; "can't": 0.31; 'to:addr:python-list': 0.32; 'received:192.168.1': 0.34; 'received:192': 0.34; 'got': 0.34; 'there': 0.35; 'header:User- Agent:1': 0.35; 'like:': 0.35; 'trigger': 0.35; 'try:': 0.35; 'doing': 0.36; 'setting': 0.36; 'received:192.168': 0.37; 'received:209.85': 0.37; 'received:google.com': 0.38; 'so,': 0.38; 'help': 0.39; 'received:209.85.214': 0.39; 'to:addr:python.org': 0.39; 'received:209': 0.39; 'except': 0.39; 'header:Received:5': 0.40; 'job?': 0.84; 'subject:handle': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:message-id:date:from:user-agent:mime-version:to :subject:references:in-reply-to:x-enigmail-version:content-type :content-transfer-encoding; bh=cwcrDtZJCmCTVZYbtZJRExiPZ1XfYc6YZwwNbhTZTTo=; b=J/mJWHLeG/OUAbu3wjFwXduI/g1lphhTY0N++QIcHWvK7ODvnTxE7iWStshWpdtagW /MOC75MLAe3WQtxlH+zdFXmdX0t4dDU3ghdRiLnrZcurQ041oV5WkdpSNPvTRe4HgN8t XWoyVY/JO57GM47fpnxPs9/ZbzfTGZ5uznNe0= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:x-enigmail-version:content-type :content-transfer-encoding; b=mr/BMQ8C9/+9YcpYhU4yQxGU9ZxXyMzozf5oZa0TyNVYWPGWgtiMw281cPiB3GCiv7 xRzrGY17vu1VOd8m6wEA1P+kJsTvoOqxg86hKtYPZr8Nwz6qMK9NFkd87sPcr++8FtLI iv1gMhB3yJvRysquGYKIWoEy4/sPLJiMCD5aY= Date: Thu, 12 May 2011 14:14:22 -0500 From: Andrew Berg User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.15) Gecko/20110303 Thunderbird/3.1.9 ThunderBrowse/3.3.5 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Proper way to handle errors in a module References: In-Reply-To: X-Enigmail-Version: 1.1.1 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 24 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1305227667 news.xs4all.nl 41110 [::ffff:82.94.164.166]:50928 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:5250 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.