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


Groups > comp.lang.python > #103765 > unrolled thread

Re: common mistakes in this simple program

Started byCameron Simpson <cs@zip.com.au>
First post2016-03-01 10:14 +1100
Last post2016-02-29 23:20 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  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

#103765 — Re: common mistakes in this simple program

FromCameron Simpson <cs@zip.com.au>
Date2016-03-01 10:14 +1100
SubjectRe: common mistakes in this simple program
Message-ID<mailman.47.1456787665.20602.python-list@python.org>
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>

[toc] | [next] | [standalone]


#103766

FromRob Gaddi <rgaddi@highlandtechnology.invalid>
Date2016-02-29 23:20 +0000
Message-ID<nb2jnf$3s7$1@dont-email.me>
In reply to#103765
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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web