common mistakes in this simple program
Cameron Simpson
cs at zip.com.au
Mon Feb 29 18:14:21 EST 2016
On 29Feb2016 10:45, Ian Kelly <ian.g.kelly at gmail.com> wrote:
>On Mon, Feb 29, 2016 at 10:26 AM, Ganesh Pal <ganesh1pal at gmail.com> wrote:
>> On Mon, Feb 29, 2016 at 9:59 PM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
>>> On Mon, Feb 29, 2016 at 8:18 AM, Ganesh Pal <ganesh1pal at 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 at zip.com.au>
More information about the Python-list
mailing list