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


Groups > comp.lang.python > #53542

Re: Newbie: use of built-in exceptions

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <cameron@cskk.homeip.net>
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; 'output': 0.05; 'context': 0.07; 'dan': 0.09; 'preferable': 0.09; 'try:': 0.09; 'cc:addr :python-list': 0.11; 'def': 0.12; '(filename,': 0.16; 'empty.': 0.16; 'exceptions,': 0.16; 'fits': 0.16; 'fp:': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'incorrect': 0.16; 'instead:': 0.16; 'message-id:@cskk.homeip.net': 0.16; 'parseerror': 0.16; 'received:211.29': 0.16; 'received:211.29.132': 0.16; 'received:cskk.homeip.net': 0.16; 'received:homeip.net': 0.16; 'received:optusnet.com.au': 0.16; 'received:syd.optusnet.com.au': 0.16; 'rules.': 0.16; 'simpson': 0.16; 'syntax,': 0.16; 'valueerror': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'file,': 0.19; "python's": 0.19; 'examples': 0.20; 'not,': 0.20; 'example': 0.22; 'cc:addr:gmail.com': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**1': 0.23; 'header:User-Agent:1': 0.23; 'cheers,': 0.24; 'define': 0.26; 'specially': 0.26; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'raise': 0.29; 'getting': 0.31; 'context,': 0.31; 'exceptions': 0.31; 'sep': 0.31; 'class': 0.32; 'cc:no real name:2**1': 0.33; 'sense': 0.34; 'skip:_ 10': 0.34; "i'd": 0.34; 'could': 0.34; 'common': 0.35; 'except': 0.35; 'usual': 0.35; 'but': 0.35; 'there': 0.35; 'received:com.au': 0.36; 'shows': 0.36; 'should': 0.36; 'half': 0.37; 'received:211': 0.38; 'pm,': 0.38; 'rather': 0.38; 'bad': 0.39; 'skip:p 20': 0.39; 'catch': 0.60; 'course.': 0.60; "you're": 0.61; 'content- disposition:inline': 0.62; 'information': 0.63; 'telling': 0.64; 'skip:\xe2 10': 0.65; 'to:addr:gmail.com': 0.65; '8bit%:43': 0.74; 'glass': 0.84; 'laid': 0.84; 'mistaken': 0.84; '2013': 0.98
Date Tue, 3 Sep 2013 10:36:42 +1000
From Cameron Simpson <cs@zip.com.au>
To Chris “Kwpolska” Warrick <kwpolska@gmail.com>
Subject Re: Newbie: use of built-in exceptions
MIME-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Disposition inline
Content-Transfer-Encoding 8bit
In-Reply-To <CAMw+j7+wyiaDxrFthss1Edh+fe-Wqb4mTt8i8xZrXBKaUjjA0g@mail.gmail.com>
User-Agent Mutt/1.5.21 (2010-09-15)
References <CAMw+j7+wyiaDxrFthss1Edh+fe-Wqb4mTt8i8xZrXBKaUjjA0g@mail.gmail.com>
X-Optus-CM-Score 0
X-Optus-CM-Analysis v=2.1 cv=bpB1Wiqi c=1 sm=1 tr=0 a=YuQlxtEQCowy2cfE5kc7TA==:117 a=YuQlxtEQCowy2cfE5kc7TA==:17 a=ZtCCktOnAAAA:8 a=PO7r1zJSAAAA:8 a=LcaDllckn3IA:10 a=zGa-SBOg9BwA:10 a=IkcTkHD0fZMA:10 a=vrnE16BAAAAA:8 a=BoUgtvrg5aEA:10 a=pGLkceISAAAA:8 a=AHvIw3bCBWROoT2rgkYA:9 a=QEXdDO2ut3YA:10 a=MSl-tDqOz04A:10
Cc python-list@python.org, rui.maciel@gmail.com
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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.525.1378168617.19984.python-list@python.org> (permalink)
Lines 39
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1378168617 news.xs4all.nl 15963 [2001:888:2000:d::a6]:45443
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:53542

Show key headers only | View raw


On 01Sep2013 13:26, Chris “Kwpolska” Warrick <kwpolska@gmail.com> wrote:
| On Sun, Sep 1, 2013 at 1:17 PM, Rui Maciel <rui.maciel@gmail.com> wrote:
| > Are there any guidelines on the use (and abuse) of Python's built-in exceptions, telling where
| > it's ok to raise them and where it's preferable to define custom exceptions instead?
| 
| There are no rules.  You should use common sense instead: if the
| exception fits your needs (eg. ValueError when incorrect output
| occurs) then use it.

A converse rule I use is: do I need to catch this specially and commonly?

My usual example is parsing: one could legitimately raise ValueError
for bad syntax, but I'd rather raise ValueError only for mistaken
calls to functions with bad values, so:

  class ParseError(ValueError):
    def __init__(self, context, complaint):
      self.context = context    # eg: file, lineno
      ValueError.__init__(self, complaint)

  def parse(filename):
    with open(filename) as fp:
      ... raise ParseError( (filename, lineno), "comma expected" )

  try:
    result = parse("datafile")
  except ParseError as e:
    ...

This also shows any reason: adding extra context information to an expection.

This is all just examples of course.

Cheers,
-- 
Cameron Simpson <cs@zip.com.au>

Whether you're getting laid or not, the glass is still half empty.
        - Dan Hillman, alt.peeves Massgasm

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


Thread

Re: Newbie: use of built-in exceptions Cameron Simpson <cs@zip.com.au> - 2013-09-03 10:36 +1000

csiph-web