Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!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:Python': 0.06; 'expressions': 0.07; 'finally:': 0.07; '"if': 0.09; 'clause': 0.09; 'except:': 0.09; 'exception,': 0.09; 'present,': 0.09; 'try:': 0.09; 'valueerror:': 0.09; 'variant': 0.09; 'python': 0.11; 'jan': 0.12; 'better?': 0.16; 'clause.': 0.16; 'exception:': 0.16; 'systemexit': 0.16; 'unexpected': 0.16; 'valueerror': 0.16; 'valueerror,': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'thu,': 0.19; 'entered': 0.20; '(or': 0.24; 'pass': 0.26; 'least': 0.26; 'asking': 0.27; 'header:In-Reply-To:1': 0.27; 'raise': 0.29; 'message-id:@mail.gmail.com': 0.30; 'bug?': 0.31; 'supposed': 0.32; 'skip:d 20': 0.34; 'could': 0.34; 'except': 0.35; 'case,': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'depends': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'catch': 0.60; 'ian': 0.60; 'first': 0.61; 'subject:The': 0.64; 'finally': 0.65; 'potentially': 0.81; '2015': 0.84; 'bare': 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=Lh8Yu12+BhfjnIOcnABaD/z/IJtlGUWUtLpDykVzlGw=; b=ZOpkiFPrQ4XaN2+BtG3ZQZdd+pO/q7sTHFjvGfDuV2+R3hRfqy+xO8c7l+Fd4zWI7r S/dECIt3PgPF9+2gThjgPmuK1fnHgh4ht0q+k8pDRvbOIkEH7CT5W34zZyTxfhxAv4fy j37xi0nqdCwapQtL2Z/nDUmt56KsKPEPtKUy2AsI2+ncU/aRGnaHEJbbFT3kWPlA/Tp5 8qwC+L6ajwILW+BwOBrDcDKZohdqYtMq6TkcqkKBggOb5WioquxalE4FCNjBMLr9ix/a jqHwu4/iABtz/MTFTJgmLCfoxIhGv8IRmc6sEJAGPmfeFNHAm8e7lKkwIdxwnZnYV2gL jNdg== X-Received: by 10.66.157.5 with SMTP id wi5mr7278795pab.37.1422609136479; Fri, 30 Jan 2015 01:12:16 -0800 (PST) MIME-Version: 1.0 In-Reply-To: <87r3uchjyg.fsf@elektro.pacujo.net> References: <20150129113238.7eaa986d@bigbox.christie.dr> <87r3uchjyg.fsf@elektro.pacujo.net> From: Ian Kelly Date: Fri, 30 Jan 2015 02:11:36 -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: 47 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1422609145 news.xs4all.nl 2858 [2001:888:2000:d::a6]:49461 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:84871 On Thu, Jan 29, 2015 at 11:16 PM, Marko Rauhamaa wrote: > Ian Kelly : > >> At least use "except Exception" instead of a bare except. Do you >> really want things like SystemExit and KeyboardInterrupt to get turned >> into 0? > > How about: > > ============================== > try: > do_interesting_stuff() > except ValueError: > try: > log_it() > except: > pass > raise > ============================== Are you asking if I think this is better? It still swallows arbitrary exceptions. Why would you want to re-raise the anticipated (and logged) ValueError instead of the exception that could potentially be unexpected? > Surprisingly this variant could raise an unexpected exception: > > ============================== > try: > do_interesting_stuff() > except ValueError: > try: > log_it() > finally: > raise > ============================== > > A Python bug? This does what it is supposed to. "If no expressions are present, raise re-raises the last exception that was active in the current scope." In this case, what that exception is depends on whether the finally clause was entered as a result of an exception or fall-through from the try clause. If you only want to re-raise the ValueError, then use the first form above. If you only want to re-raise the other exception, then do so from an except block (or don't catch it in the first place).