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


Groups > comp.lang.python > #103766

Re: common mistakes in this simple program

From Rob Gaddi <rgaddi@highlandtechnology.invalid>
Newsgroups comp.lang.python
Subject Re: common mistakes in this simple program
Date 2016-02-29 23:20 +0000
Organization A noiseless patient Spider
Message-ID <nb2jnf$3s7$1@dont-email.me> (permalink)
References <CALwzidn8-HDrGSqZT2ODxhdNYL7yFt5hdmC8RnB4WC=gkD4Wtg@mail.gmail.com> <mailman.47.1456787665.20602.python-list@python.org>

Show all headers | View raw


Cameron Simpson wrote:

> 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>

Or, for that matter:

  logging.exception('something went wrong')

Which gives you the whole traceback as well and doesn't require you to
explictly grab the exception.

-- 
Rob Gaddi, Highland Technology -- www.highlandtechnology.com

Email address domain is currently out of order.  See above to fix.

Back to comp.lang.python | Previous | NextPrevious 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