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


Groups > comp.lang.python > #103765

Re: common mistakes in this simple program

From Cameron Simpson <cs@zip.com.au>
Newsgroups comp.lang.python
Subject Re: common mistakes in this simple program
Date 2016-03-01 10:14 +1100
Message-ID <mailman.47.1456787665.20602.python-list@python.org> (permalink)
References <CALwzidn8-HDrGSqZT2ODxhdNYL7yFt5hdmC8RnB4WC=gkD4Wtg@mail.gmail.com>

Show all headers | View raw


On 29Feb2016 10:45, Ian Kelly <ian.g.kelly@gmail.com> wrote:
>On Mon, Feb 29, 2016 at 10:26 AM, Ganesh Pal <ganesh1pal@gmail.com> wrote:
>> On Mon, Feb 29, 2016 at 9:59 PM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
>>> On Mon, Feb 29, 2016 at 8:18 AM, Ganesh Pal <ganesh1pal@gmail.com> 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 <cs@zip.com.au>

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


Thread

Re: common mistakes in this simple program Cameron Simpson <cs@zip.com.au> - 2016-03-01 10:14 +1100
  Re: common mistakes in this simple program Rob Gaddi <rgaddi@highlandtechnology.invalid> - 2016-02-29 23:20 +0000

csiph-web