Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Cameron Simpson Newsgroups: comp.lang.python Subject: Re: common mistakes in this simple program Date: Tue, 1 Mar 2016 10:14:21 +1100 Lines: 31 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed X-Trace: news.uni-berlin.de 1gnctexR3vVesHUo+pstoA/V2YwWqiVzxf2Roq4jqJqw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'context': 0.05; 'exception.': 0.07; '%s",': 0.09; 'exception:': 0.09; 'handled.': 0.09; 'python': 0.10; 'exception': 0.13; '10:26': 0.16; '2016': 0.16; '>on': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'message- id:@cskk.homeip.net': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'reraise': 0.16; 'simpson': 0.16; 'subject:program': 0.16; 'subject:simple': 0.16; 'wrote:': 0.16; '>>>': 0.20; 'arguments': 0.22; 'cheers,': 0.22; 'am,': 0.23; 'feb': 0.23; 'header:In-Reply-To:1': 0.24; 'mon,': 0.24; 'header :User-Agent:1': 0.26; 'example': 0.26; 'equivalent': 0.27; 'function': 0.28; 'went': 0.28; 'raise': 0.29; 'code': 0.30; 'another': 0.32; 'generally': 0.32; 'handle': 0.34; 'something': 0.35; 'but': 0.36; 'should': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'expect': 0.37; 'charset:us-ascii': 0.37; 'things': 0.38; 'received:localdomain': 0.38; 'log': 0.38; 'files': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'further': 0.62; 'here:': 0.63; 'cameron': 0.66; 'accompanying': 0.67; 'remark': 0.84; 'subject:common': 0.84; 'to:name:python': 0.84; 'subject:this': 0.85 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.24 (2015-08-30) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:103765 On 29Feb2016 10:45, Ian Kelly wrote: >On Mon, Feb 29, 2016 at 10:26 AM, Ganesh Pal wrote: >> On Mon, Feb 29, 2016 at 9:59 PM, Ian Kelly wrote: >>> On Mon, Feb 29, 2016 at 8:18 AM, Ganesh Pal wrote: >>>> 1. usage of try- expect >>> >>> try-except in every single function is a code smell. You should only >>> be using it where you're actually going to handle the exception. If >>> you catch an exception just to log it, you generally should also >>> reraise it so that something further up the call chain has the >>> opportunity to handle it. >> >> How do we reraise the exception in python , I have used raise not >> sure how to reraise the exception > >raise with no arguments will reraise the exception currently being handled. > >except Exception: > logging.error("something went wrong") > raise Another remark here: if you're going to log, log the exception as well: logging.error("something went wrong: %s", e) Ian's example code is nice and simple to illustrate "log and then reraise" but few things are as annoying as log files reciting "something went wrong" or the equivalent without any accompanying context information. Cheers, Cameron Simpson