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


Groups > comp.lang.python > #98136

Re: Modern recommended exception handling practices?

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: Modern recommended exception handling practices?
Date Tue, 3 Nov 2015 18:16:18 +1100
Lines 94
Message-ID <mailman.7.1446534982.8789.python-list@python.org> (permalink)
References <52739457-5f7a-48ea-8835-9fc8934174f9@googlegroups.com> <56385887$0$1598$c3e8da3$5496439d@news.astraweb.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de wk9gp0yAPea1IROY3vFfTgBW13BZ75DTIRlJfaw9C30Q==
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'received:209.85.223': 0.03; 'handler': 0.04; 'exception.': 0.07; 'nasty': 0.07; 'obsolete': 0.07; 'though:': 0.07; 'type,': 0.07; 'cc:addr:python-list': 0.09; '"or': 0.09; 'dict': 0.09; 'handler,': 0.09; 'instance.': 0.09; 'lookup': 0.09; 'python:': 0.09; 'spelled': 0.09; 'zero.': 0.09; 'url:blog': 0.10; 'python': 0.10; 'python.': 0.11; 'exception': 0.13; 'useful,': 0.13; 'clauses': 0.16; 'disallow': 0.16; 'exception?': 0.16; 'found"': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'idx': 0.16; "key's": 0.16; 'keyerror': 0.16; 'occur.': 0.16; 'practices.': 0.16; 'presence.': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'rough': 0.16; 'something.': 0.16; 'subject:exception': 0.16; 'subject:handling': 0.16; 'wrote:': 0.16; 'string': 0.17; 'skip:{ 20': 0.18; 'string,': 0.18; 'try:': 0.18; '(in': 0.18; 'input': 0.18; 'versions': 0.20; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'exceptions': 0.22; 'keyboard': 0.22; 'keyerror:': 0.22; 'keys': 0.22; 'pass': 0.22; 'am,': 0.23; 'header:In-Reply-To:1': 0.24; 'testing': 0.25; 'figure': 0.27; 'fri,': 0.27; 'handling': 0.27; 'message- id:@mail.gmail.com': 0.27; 'object,': 0.27; 'function': 0.28; '"no': 0.29; 'another.': 0.29; 'boundary': 0.29; 'catching': 0.29; 'exists,': 0.29; 'faster,': 0.29; 'measure': 0.29; 'once.': 0.29; 'searches': 0.29; 'raise': 0.29; 'code': 0.30; 'becomes': 0.30; 'mention': 0.30; 'operations': 0.31; 'probably': 0.31; "can't": 0.32; 'aside': 0.32; 'language.': 0.32; 'possibly': 0.32; 'url:python': 0.33; "d'aprano": 0.33; 'null': 0.33; 'rule': 0.33; 'steven': 0.33; 'case,': 0.34; 'know.': 0.34; 'recommended': 0.34; 'tue,': 0.34; 'except': 0.34; 'server': 0.34; 'list': 0.34; 'received:google.com': 0.35; 'conditions.': 0.35; 'nov': 0.35; 'returning': 0.35; 'item': 0.35; 'but': 0.36; 'should': 0.36; 'instead': 0.36; 'there': 0.36; 'received:209.85': 0.36; '(i.e.': 0.36; 'cases': 0.36; 'flow': 0.36; 'subject:?': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'being': 0.37; 'agree': 0.37; 'say': 0.37; 'done.': 0.37; 'missing': 0.37; 'list.': 0.37; 'experience,': 0.38; 'received:209': 0.38; 'log': 0.38; 'means': 0.39; 'why': 0.39; 'where': 0.40; 'some': 0.40; 'ten': 0.60; 'your': 0.60; 'avoid': 0.61; 'more': 0.63; 'times': 0.63; 'you.': 0.64; 'places': 0.64; 'between': 0.65; 'rare': 0.66; 'jobs': 0.67; '(5)': 0.72; 'race': 0.72; 'evaluate': 0.72; 'sounds': 0.76; '"look': 0.84; 'actually,': 0.84; 'chrisa': 0.84; 'costly': 0.84; 'divide': 0.84; 'subject:recommended': 0.84; 'ten,': 0.84; 'tribute': 0.84; 'absolutely': 0.88; 'to:none': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=dH8ZW2uM07xtTM/0x+hOFqNBJ9vMCsSVGoLsY6hVKqw=; b=C+glIUn6NRH5F7z1wmrD6cVPeLTcTirIqc3IcfQQ0kebsutOkbBAj+xVI5BSQDFyvP QtqqIVqzQgwbutV3WVaCV1c9eJ2i8/MFmzzrvhJfmmaoua+PyFNxD56Sqb5NFb4Rjipf URWmgFyXSE9inBKadLU5LvQ04SLoclhDxzTCNLFBiW5pQ6c7Xr2m7H0O8ZwvkmDaZjyM OwZkLltN4hVnsx86LvzwloVvjQVJhY4vNkAxS7tHnHVLGPbqFzU6SB1Sb+MGI0WJrHqO Nxem71HZ3NHP9HTU84YBO5JnmT/S82fYMbPo2eyZvIKBzSMRnT7PgHdt+AdYfBzS9hgm q0OQ==
X-Received by 10.107.34.149 with SMTP id i143mr24748071ioi.157.1446534978588; Mon, 02 Nov 2015 23:16:18 -0800 (PST)
In-Reply-To <56385887$0$1598$c3e8da3$5496439d@news.astraweb.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:98136

Show key headers only | View raw


On Tue, Nov 3, 2015 at 5:47 PM, Steven D'Aprano <steve@pearwood.info> wrote:
> On Fri, 30 Oct 2015 04:43 am, vasudevram wrote:
>
>> Are there any modern (i.e. for current versions of Python 2 and 3)
>> recommended exception handling practices?
>
> When you say "modern", it sounds like you want to contrast them with "old
> fashioned and obsolete" exception-handling practices. I don't know if there
> are any obsolete exception-handling practices in Python.

Aside from string exceptions and the "except Type, e:" syntax, I would
agree with you. Actually, I can't think of any "obsolete
exception-handling practices" in any language. Exception handling is
pretty straight-forward: you raise an exception in one place, and you
catch it in another.

> Bare `except` clauses are very possibly *literally the worst* thing that you
> can write in Python:
>
> https://realpython.com/blog/python/the-most-diabolical-python-antipattern/

I would actually like to disallow the bare except, in the same way
that I would disallow the Py2 input() function. In the rare cases
where you really do want to catch *every* exception (eg at a boundary
between a web server and a request handler, where you would log the
exception and return a 500), it should be spelled "except
BaseException [as e]:", same as you should use "eval(raw_input())" in
those extremely rare cases where you actually want to take keyboard
input and evaluate it.

But more generally, the over-broad exception handler is a nasty anti-pattern.

> (5) Remember that often you can avoid exceptions instead of catching
> them. "Look Before You Leap" (LBYL) may be a perfectly good alternative:
>
>     if item in mylist:
>         idx = mylist.index(item)
>         process(idx)
>     else:
>         result = "not found"
>
>
> but be sensitive to the amount of work done. The above code searches the
> list twice instead of just once.

Not to mention having race condition possibilities. There are a few
places where this is useful, though:

start_time = time()
work_done = do_some_work()
time_spent = time()-start_time or 1
print(f"Did {work_done} jobs in {time_spent} secs: {work_done/time_spent} j/s")

The "or 1" is a quick check that means we don't divide by zero. The
performance figure becomes meaningless, but if this is a rare case,
that's probably fine.

> (7) So which is faster, LBYL or catching the exception? That is extremely
> sensitive to not just the specific operations being performed, but how
> often the exceptional cases occur. In general, you must measure your code
> to know.
>
> But as a very rough rule of thumb, consider looking up a key in a dict:
>
>     if key in mydict:
>         result = mydict[key]
>
> versus
>
>     try:
>         result = mydict[key]
>     except KeyError:
>         pass
>
>
> In my experience, catching the KeyError is about ten times more costly than
> testing for the key's presence. So if your keys are missing more than one
> time in ten, it's probably better to use LBYL.

This is actually a tribute to dict performance for key lookup (since
that's one of the important operations in a hashtable). It's NOT the
case for a list.


Exceptions are the "other way" to return something. In a function that
always returns a string, returning None is distinguishable (in the
same way that a C function can return a NULL pointer); in a function
that returns absolutely any object, the only way to signal "no object
to return" is to raise an exception. That's why StopIteration exists,
for instance. Exceptions are a normal part of program flow - they
signal an "exceptional condition" in some small area, but it's normal
to cope with exceptional conditions.

ChrisA

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


Thread

Modern recommended exception handling practices? vasudevram <vasudevram@gmail.com> - 2015-10-29 10:43 -0700
  Re: Modern recommended exception handling practices? Steven D'Aprano <steve@pearwood.info> - 2015-11-03 17:47 +1100
    Re: Modern recommended exception handling practices? Chris Angelico <rosuav@gmail.com> - 2015-11-03 18:16 +1100
      Re: Modern recommended exception handling practices? Steven D'Aprano <steve@pearwood.info> - 2015-11-03 18:22 +1100
        Re: Modern recommended exception handling practices? Chris Angelico <rosuav@gmail.com> - 2015-11-03 18:52 +1100
      Re: Modern recommended exception handling practices? Paul Rubin <no.email@nospam.invalid> - 2015-11-03 11:59 -0800

csiph-web