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


Groups > comp.lang.python > #88846

Re: try..except with empty exceptions

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <cameron@cskk.homeip.net>
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; 'syntax': 0.04; 'anyway.': 0.05; 'classes,': 0.05; 'root': 0.05; 'skip:` 10': 0.07; 'tries': 0.07; 'string': 0.09; 'agree,': 0.09; 'except:': 0.09; 'exception,': 0.09; 'feature,': 0.09; 'forcing': 0.09; 'prefix': 0.09; 'raises': 0.09; 'semantic': 0.09; 'spelled': 0.09; 'try:': 0.09; 'used.': 0.09; 'python': 0.11; 'def': 0.12; 'thread': 0.14; "'''": 0.16; '*a,': 0.16; '>on': 0.16; 'advantage.': 0.16; 'class:': 0.16; 'codebases.': 0.16; 'daemons': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'ideally,': 0.16; 'imo,': 0.16; 'message-id:@cskk.homeip.net': 0.16; 'operation.': 0.16; 'oppose': 0.16; 'portable': 0.16; 'simpson': 0.16; 'sorts': 0.16; 'succinct': 0.16; 'tending': 0.16; 'terribly': 0.16; 'exception': 0.16; 'fix': 0.17; 'wrote:': 0.18; '>>>': 0.22; 'code,': 0.22; 'example': 0.22; 'to:name:python-list@python.org': 0.22; 'header :User-Agent:1': 0.23; 'config': 0.24; 'skip:l 30': 0.24; 'unicode': 0.24; 'cheers,': 0.24; 'values': 0.27; 'header:In- Reply-To:1': 0.27; 'idea': 0.28; 'point': 0.28; 'chris': 0.29; 'am,': 0.29; 'then.': 0.30; "i'm": 0.30; 'code': 0.31; '(usually': 0.31; 'catching': 0.31; 'exceptions': 0.31; 'ones.': 0.31; 'operations.': 0.31; 'routine': 0.31; 'class': 0.32; 'this.': 0.32; 'run': 0.32; 'guess': 0.33; 'third': 0.33; 'could': 0.34; 'subject:with': 0.35; "can't": 0.35; 'common': 0.35; 'but': 0.35; 'add': 0.35; 'there': 0.35; 'charset:us-ascii': 0.36; "i'll": 0.36; 'bringing': 0.38; 'skip:m 40': 0.38; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'that,': 0.38; 'recent': 0.39; 'does': 0.39; '12,': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'remove': 0.60; 'catch': 0.60; 'serving': 0.60; 'truly': 0.60; 'eye': 0.61; "you're": 0.61; 'content- disposition:inline': 0.62; "you'll": 0.62; 'personal': 0.63; 'more': 0.64; 'occur': 0.65; 'world': 0.66; 'believe': 0.68; 'wish': 0.70; '*and*': 0.84; '2015': 0.84; 'bare': 0.84; 'common,': 0.84; 'confusing': 0.84; 'everything,': 0.84; 'everything.': 0.84; 'noise': 0.84; 'pools': 0.84; 'received:192.168.15': 0.84; 'routines': 0.84; 'subject:..': 0.84; 'absolutely': 0.87; 'few.': 0.91; 'why?': 0.91
Date Sun, 12 Apr 2015 09:08:00 +1000
From Cameron Simpson <cs@zip.com.au>
To "python-list@python.org" <python-list@python.org>
Subject Re: try..except with empty exceptions
MIME-Version 1.0
Content-Type text/plain; charset=us-ascii; format=flowed
Content-Disposition inline
In-Reply-To <CAPTjJmrVn5w-ix2yoDS0aPKm9krmAO3B8MmdpBNqhevdkhENCw@mail.gmail.com>
User-Agent Mutt/1.5.23 (2014-03-12)
References <CAPTjJmrVn5w-ix2yoDS0aPKm9krmAO3B8MmdpBNqhevdkhENCw@mail.gmail.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>
Newsgroups comp.lang.python
Message-ID <mailman.236.1428793684.12925.python-list@python.org> (permalink)
Lines 88
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1428793684 news.xs4all.nl 2914 [2001:888:2000:d::a6]:41575
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:88846

Show key headers only | View raw


On 12Apr2015 07:52, Chris Angelico <rosuav@gmail.com> wrote:
>On Sun, Apr 12, 2015 at 7:37 AM, Cameron Simpson <cs@zip.com.au> wrote:
>> On 11Apr2015 21:21, Chris Angelico <rosuav@gmail.com> wrote:
>>> But I agree, it would be very nice if Python 3 could have abolished
>>> the truly confusing part of this, where "except:" catches everything.
>>> Forcing people to spell it "except BaseException:" would fix all of
>>> this. How hard is it to deprecate and then remove that, same as string
>>> exceptions were removed?
>>
>> I guess I'll go over there to oppose it then.
>>
>> Why? It makes it harder to write portable python 2/3 code and does not add
>> any semantic advantage.
>>
>> Unless there's a common root exception class in Python 2, which I believe
>> there isn't, you can't catch all exceptions in python 2 without the
>> "except:" syntax.  Which means the _only_ way to have some code in both 2
>> and 3 that does it requires 2 codebases.
>>
>> As one who tries to have his code run in both 2 (usually recent 2, like
>> 2.6/2.7) and 3, this change would cause a signification breakage for me
>> without bringing any semantic benefits.
>
>Can you give an example of a place where in Py2 you absolutely have to
>catch everything, and don't have control over the code, *and* are
>trying to do a one-codebase routine with Py3 compatibility? If you're
>trying for 2/3 compatibility, you'll need to have all your exceptions
>derive from BaseException anyway.

I don't make many personal exception classes, tending to reuse stdlib ones. I'm 
sure I have a few.

But regarding codebase:

    [hg/css]fleet*> g except: **/*.py
    lib/python/cs/app/pilfer.py:664:              except:
    lib/python/cs/asynchron.py:145:    except:
    lib/python/cs/db.py:184:      except:
    lib/python/cs/excutils.py:34:  except:
    lib/python/cs/fileutils.py:69:  except:
    lib/python/cs/idset.py:46:      except:
    lib/python/cs/later.py:156:    except:
    lib/python/cs/mailutils.py:274:      except:
    lib/python/cs/nodedb/tokcab.py:57:    except:
    lib/python/cs/queues.py:441:          except:
    lib/python/cs/queues.py:458:              except:
    lib/python/cs/threads.py:131:      except:

Catching all exceptions isn't terribly common, _except_ in service routines 
that wrap "unknown" operations. Classic example from my Asynchron class:

    def call(self, func, *a, **kw):
      ''' Have the Asynchron call `func(*a,**kw)` and store its values as
          self.result.
          If `func` raises an exception, store it as self.exc_info.
      '''
      try:
        r = func(*a, **kw)
      except:
        self.exc_info = sys.exc_info
      else:
        self.result = r

All sorts of things like thread pools and other "worker" functions, and 
run-forever daemons like mail filers that can have arbitrary exceptions occur 
in (partly) third party code eg from config files; you need to catch any 
unknown exception and fail the specific action, but continue the main daemon 
operation.

And since I use this code in Python 2, and since not all exceptions are 
BaseException subclasses, I need the bare syntax.

Also, IMO, a bare "except:" syntax is _far_ more pleasing to the eye than 
"except magic_exception_name_that+gets_everything:". Also, I wish 
"BaseException" were just spelled "Exception", if it has to be used.

>At very worst, it could be turned into a compat-only syntax feature,
>like the u"spam" noise prefix on Unicode strings - serving absolutely
>no purpose in Py3 code, and ideally, able to be removed at some point
>post-2020.

I'm -0.1 on the idea myself. I consider "except:" succinct and evocative, and 
prefer it to "except BaseException:".

Cheers,
Cameron Simpson <cs@zip.com.au>

On a videophone, the whole world can see you fart.      - Charlie Stross

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


Thread

Re: try..except with empty exceptions Cameron Simpson <cs@zip.com.au> - 2015-04-12 09:08 +1000
  Re: try..except with empty exceptions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-04-12 14:18 +1000
    Re: try..except with empty exceptions Cameron Simpson <cs@zip.com.au> - 2015-04-12 16:47 +1000

csiph-web