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: 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: <20150129113238.7eaa986d@bigbox.christie.dr> <87r3uchjyg.fsf@elektro.pacujo.net> <87r3ucljyv.fsf@elektro.pacujo.net> <87fvaslh9h.fsf@elektro.pacujo.net> <87zj90jngf.fsf@elektro.pacujo.net> <87vbjojm86.fsf@elektro.pacujo.net> From: Ian Kelly Date: Fri, 30 Jan 2015 09:38:53 -0700 Subject: Re: The Most Diabolical Python Antipattern To: Python 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Fri, Jan 30, 2015 at 8:56 AM, Marko Rauhamaa wrote: > Ian Kelly : > >> 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