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


Groups > comp.lang.python > #21530

Re: Raise X or Raise X()?

Path csiph.com!usenet.pasdenom.info!news.chainon-marquant.org!nntpfeed.proxad.net!proxad.net!feeder2-2.proxad.net!82.197.223.106.MISMATCH!feeder1.cambriumusenet.nl!feeder2.cambriumusenet.nl!feed.tweaknews.nl!194.109.133.85.MISMATCH!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; 'preferably': 0.03; 'classes.': 0.05; 'exception,': 0.07; 'rules.': 0.07; 'python': 0.08; 'argument,': 0.09; 'conventions.': 0.09; 'exceptions': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'underlying': 0.09; 'am,': 0.12; 'exception': 0.12; 'classes,': 0.13; 'both.': 0.16; 'enigma': 0.16; 'exceptions.': 0.16; 'kern': 0.16; 'syntax': 0.16; 'wrote:': 0.18; "they've": 0.18; "aren't": 0.21; 'header:In- Reply-To:1': 0.22; 'wonder': 0.23; 'though.': 0.23; 'preferred': 0.25; 'raise': 0.28; 'version,': 0.28; "i'm": 0.28; 'pass': 0.29; 'interpret': 0.29; 'class': 0.29; 'print': 0.29; 'bare': 0.30; 'cases': 0.32; "i've": 0.32; 'break': 0.32; 'there': 0.33; 'header :User-Agent:1': 0.33; 'header:X-Complaints-To:1': 0.34; 'done': 0.34; 'rather': 0.34; 'calling': 0.34; 'it."': 0.34; 'try:': 0.34; 'to:addr:python-list': 0.35; '...': 0.35; 'form,': 0.35; 'regular': 0.35; 'received:org': 0.36; 'enough': 0.38; 'allows': 0.38; 'should': 0.38; 'except': 0.39; 'to:addr:python.org': 0.40; 'world': 0.61; 'received:86': 0.63; 'our': 0.63; 'believe': 0.65; 'special': 0.66; 'favor': 0.70; '"some': 0.84; 'fulfils': 0.84; 'it"': 0.84; 'zen': 0.84; 'eco': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Robert Kern <robert.kern@gmail.com>
Subject Re: Raise X or Raise X()?
Date Mon, 12 Mar 2012 12:47:27 +0000
References <387014.2537.1331492695725.JavaMail.geo-discussion-forums@pbjv6> <4F5DD1FC.7080603@sequans.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host host86-161-237-213.range86-161.btcentralplus.com
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:10.0.2) Gecko/20120216 Thunderbird/10.0.2
In-Reply-To <4F5DD1FC.7080603@sequans.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
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.584.1331556465.3037.python-list@python.org> (permalink)
Lines 41
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1331556465 news.xs4all.nl 6981 [2001:888:2000:d::a6]:35484
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:21530

Show key headers only | View raw


On 3/12/12 10:37 AM, Jean-Michel Pichavant wrote:
> bvdp wrote:
>> Which is preferred in a raise: X or X()? I've seen both. In my specific case
>> I'm dumping out of a deep loop:
>>
>> try:
>> for ...
>> for ...
>> for ...
>> if match:
>> raise StopInteration()
>> else ...
>>
>> except StopInteration:
>> print "found it"
>
> I prefer the raise X() version, it fulfils the zen of python :
>
> "Special cases aren't special enough to break the rules.
> There should be one-- and preferably only one --obvious way to do it."
>
> I still wonder why they've added the class raise form, on which purpose.

The class raise form used to be the only way to raise exceptions. To pass an 
argument, there was special syntax:

   raise Exception, "some message"

This syntax has been done away with in Python 3 in favor of regular calling 
conventions. Python 3 still allows bare classes, though. I also prefer to always 
raise instances of exceptions rather than bare exception classes. It simplifies 
the mental model.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco

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


Thread

Raise X or Raise X()? bvdp <bob@mellowood.ca> - 2012-03-11 12:04 -0700
  Re: Raise X or Raise X()? Irmen de Jong <irmen.NOSPAM@xs4all.nl> - 2012-03-11 21:37 +0100
    Re: Raise X or Raise X()? Chris Rebert <clp2@rebertia.com> - 2012-03-11 14:49 -0700
    Re: Raise X or Raise X()? Stefan Behnel <stefan_ml@behnel.de> - 2012-03-12 14:52 +0100
      Re: Raise X or Raise X()? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-03-12 15:08 +0000
        Re: Raise X or Raise X()? Stefan Behnel <stefan_ml@behnel.de> - 2012-03-12 17:08 +0100
  Re: Raise X or Raise X()? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-03-11 23:59 +0000
    Re: Raise X or Raise X()? bvdp <bob@mellowood.ca> - 2012-03-11 18:53 -0700
    Re: Raise X or Raise X()? MRAB <python@mrabarnett.plus.com> - 2012-03-12 02:26 +0000
  Re: Raise X or Raise X()? Jean-Michel Pichavant <jeanmichel@sequans.com> - 2012-03-12 11:37 +0100
  Re: Raise X or Raise X()? Robert Kern <robert.kern@gmail.com> - 2012-03-12 12:47 +0000
  Re: Raise X or Raise X()? James Elford <fil.oracle@gmail.com> - 2012-03-12 13:06 +0000
  Re: Raise X or Raise X()? Chris Angelico <rosuav@gmail.com> - 2012-03-13 00:13 +1100

csiph-web