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


Groups > comp.lang.python > #103766

Re: common mistakes in this simple program

Path csiph.com!feeder.erje.net!2.eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail
From Rob Gaddi <rgaddi@highlandtechnology.invalid>
Newsgroups comp.lang.python
Subject Re: common mistakes in this simple program
Date Mon, 29 Feb 2016 23:20:15 -0000 (UTC)
Organization A noiseless patient Spider
Lines 44
Message-ID <nb2jnf$3s7$1@dont-email.me> (permalink)
References <CALwzidn8-HDrGSqZT2ODxhdNYL7yFt5hdmC8RnB4WC=gkD4Wtg@mail.gmail.com> <mailman.47.1456787665.20602.python-list@python.org>
Mime-Version 1.0
Content-Type text/plain; charset=US-ASCII
Content-Transfer-Encoding 7bit
Injection-Date Mon, 29 Feb 2016 23:20:15 -0000 (UTC)
Injection-Info mx02.eternal-september.org; posting-host="0359355f8ebefe2d2daa8a26e0c5d98b"; logging-data="3975"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+LpAQ17+LXkGfoBHUX5B24"
User-Agent XPN/1.2.6 (Street Spirit ; Linux)
Cancel-Lock sha1:IIJuZgZMdmeEzSyhqDJGMNwffq0=
Xref csiph.com comp.lang.python:103766

Show key headers only | 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