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


Groups > comp.lang.python > #84902

Re: The Most Diabolical Python Antipattern

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
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; 'else:': 0.03; 'handler': 0.05; 'subject:Python': 0.06; 'finally:': 0.07; 'propagate': 0.09; 'try:': 0.09; 'valueerror:': 0.09; 'wrong,': 0.09; 'jan': 0.12; 'chained': 0.16; 'elsewhere,': 0.16; 'exception:': 0.16; 'situation.': 0.16; 'valueerror': 0.16; 'valueerror,': 0.16; 'exception': 0.16; 'all.': 0.16; 'wrote:': 0.18; 'order.': 0.26; 'suggested': 0.26; 'header:In-Reply-To:1': 0.27; 'specifically': 0.29; 'am,': 0.29; 'raise': 0.29; "doesn't": 0.30; 'message- id:@mail.gmail.com': 0.30; 'went': 0.31; 'code': 0.31; 'that.': 0.31; 'exceptions': 0.31; 'class': 0.32; 'fri,': 0.33; 'skip:d 20': 0.34; 'could': 0.34; 'except': 0.35; 'received:google.com': 0.35; 'should': 0.36; 'depends': 0.38; 'handle': 0.38; 'to:addr :python-list': 0.38; 'to:addr:python.org': 0.39; 'how': 0.40; 'catch': 0.60; 'chain': 0.60; 'ian': 0.60; 'subject:The': 0.64; '30,': 0.65; '2015': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=6sikZbEuabTHP0XqZ0sN4gepu1SK4cHKiZKWKzwY3yk=; b=Qe84TCf5k0JwoBGVfJuD/EW8evHOjxqnw9KRuXDz+ichkX4SMHTAOze++30hf+zumQ ZHSX8mgqqbD7chjCjC2FzSBjlwOiH2SzOJRBrx0vrvI/V0d8BwbqR4dulpkJA3GXgPyq 9BXzBNdQZi5+m3oPmf7u5cV15qtNyUMEf2+fO60sXyWqdAgxNk0LJk3+jkxc2Yt9sLFg eEGuRntpEiSASoFXCV42rpMSj5RFjfrw3mJhnahGrNhWLj2aHT5yTZMX9bmecYax3pO8 1hCO0aHhS9Ogb7D7JnEBTV6iWb3XjpCsn6G9BSccjbimd+K1mg5OnufT1Igq0YUt6xTP fIBg==
X-Received by 10.68.68.145 with SMTP id w17mr10215941pbt.82.1422635974277; Fri, 30 Jan 2015 08:39:34 -0800 (PST)
MIME-Version 1.0
In-Reply-To <87vbjojm86.fsf@elektro.pacujo.net>
References <madpuv$he7$1@ger.gmane.org> <20150129113238.7eaa986d@bigbox.christie.dr> <mailman.18286.1422571931.18130.python-list@python.org> <87r3uchjyg.fsf@elektro.pacujo.net> <mailman.18296.1422605482.18130.python-list@python.org> <87r3ucljyv.fsf@elektro.pacujo.net> <87fvaslh9h.fsf@elektro.pacujo.net> <mailman.18306.1422631319.18130.python-list@python.org> <87zj90jngf.fsf@elektro.pacujo.net> <mailman.18307.1422632572.18130.python-list@python.org> <87vbjojm86.fsf@elektro.pacujo.net>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Fri, 30 Jan 2015 09:38:53 -0700
Subject Re: The Most Diabolical Python Antipattern
To Python <python-list@python.org>
Content-Type text/plain; charset=UTF-8
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 <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.18312.1422635977.18130.python-list@python.org> (permalink)
Lines 41
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1422635978 news.xs4all.nl 2901 [2001:888:2000:d::a6]:33535
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:84902

Show key headers only | View raw


On Fri, Jan 30, 2015 at 8:56 AM, Marko Rauhamaa <marko@pacujo.net> wrote:
> Ian Kelly <ian.g.kelly@gmail.com>:
>
>> Like I suggested earlier, just don't catch the inner exception at all.
>> The result will be both exceptions propagated, chained in the proper
>> order.
>
> Depends on the situation.

Like what? If you want to specifically propagate the original
exception in order to be caught again elsewhere, then I think there's
a code smell to that. If this inner exception handler doesn't
specifically know how to handle a ValueError, then why should some
outer exception handler be able to handle an exception that could have
come from virtually anywhere?

A better approach to that would be to create a specific exception
class that narrowly identifies what went wrong, and raise *that* with
the other exceptions chained to it. E.g.:

  try:
      do_interesting_stuff()
  except ValueError as e:
      try:
          log_it()
      except Exception:
          # Chain both exceptions as __context__
          raise SpecificException
      else:
          # Chain the original exception as __cause__
          raise SpecificException from e

Or if you don't care about distinguishing __cause__ from __context__:

  try:
      do_interesting_stuff()
  except ValueError:
      try:
          log_it()
      finally:
          raise SpecificException

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


Thread

Re: The Most Diabolical Python Antipattern Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-29 15:51 -0700
  Re: The Most Diabolical Python Antipattern Marko Rauhamaa <marko@pacujo.net> - 2015-01-30 08:16 +0200
    Re: The Most Diabolical Python Antipattern Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-01-30 08:10 +0000
      Re: The Most Diabolical Python Antipattern Marko Rauhamaa <marko@pacujo.net> - 2015-01-30 11:02 +0200
        Re: The Most Diabolical Python Antipattern Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-30 02:17 -0700
        Re: The Most Diabolical Python Antipattern Marko Rauhamaa <marko@pacujo.net> - 2015-01-30 12:00 +0200
          Re: The Most Diabolical Python Antipattern Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-30 08:21 -0700
            Re: The Most Diabolical Python Antipattern Marko Rauhamaa <marko@pacujo.net> - 2015-01-30 17:30 +0200
              Re: The Most Diabolical Python Antipattern Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-30 08:42 -0700
                Re: The Most Diabolical Python Antipattern Marko Rauhamaa <marko@pacujo.net> - 2015-01-30 17:56 +0200
                Re: The Most Diabolical Python Antipattern Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-30 09:38 -0700
              Re: The Most Diabolical Python Antipattern Chris Angelico <rosuav@gmail.com> - 2015-01-31 02:55 +1100
    Re: The Most Diabolical Python Antipattern Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-30 02:11 -0700
    Re: The Most Diabolical Python Antipattern Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-01-31 01:28 +0000

csiph-web