Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!news.redatomik.org!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'syntax': 0.04; 'skip:` 10': 0.07; 'clause': 0.09; 'equivalent;': 0.09; 'except:': 0.09; 'exception,': 0.09; 'forcing': 0.09; 'raises': 0.09; 'spelled': 0.09; 'try:': 0.09; 'used.': 0.09; 'cc:addr :python-list': 0.11; 'python': 0.11; 'def': 0.12; 'thread': 0.14; "wouldn't": 0.14; "'''": 0.16; '*a,': 0.16; 'class:': 0.16; 'daemons': 0.16; 'enough.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'imo,': 0.16; 'mandate': 0.16; 'old-style': 0.16; 'operation.': 0.16; 'simpson': 0.16; 'sorts': 0.16; 'succinct': 0.16; 'systemexit': 0.16; 'terribly': 0.16; 'x.y': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'module': 0.19; 'example': 0.22; 'cc:addr:python.org': 0.22; 'case.': 0.24; 'config': 0.24; 'cc:2**0': 0.24; 'least': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'point': 0.28; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'catching': 0.31; 'exceptions': 0.31; 'operations.': 0.31; 'stuff': 0.32; 'up.': 0.33; 'guess': 0.33; 'third': 0.33; 'could': 0.34; 'subject:with': 0.35; 'classes': 0.35; 'except': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'doing': 0.36; 'should': 0.36; 'too': 0.37; 'being': 0.38; 'skip:m 40': 0.38; 'that,': 0.38; '12,': 0.39; 'how': 0.40; 'catch': 0.60; 'most': 0.60; 'eye': 0.61; 'simply': 0.61; "you're": 0.61; 'name': 0.63; 'more': 0.64; 'occur': 0.65; 'here': 0.66; 'fact,': 0.69; 'wish': 0.70; 'therefore': 0.72; 'unusual': 0.74; '2015': 0.84; 'bare': 0.84; 'common,': 0.84; 'everything,': 0.84; 'pools': 0.84; 'routines': 0.84; 'subject:..': 0.84; 'absolutely': 0.87; 'to:none': 0.92 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=FafjdTZi8qv8b7tYl+eVCpHDG1BdcPnhPoakwAsZNPs=; b=xEru+a4cV4758n67jPAVEsVprqFuoSgKQl5pfkJmzEBFxqnNEWAnuS5jGtkIgA3ppN Kv1aYwtWKCAJMS1w1TGJZj4/bYcaY3jjtx/08oj5H+JUSxSN9XhdWpaCP9TBsh+bbfXy HiWR7mbXJ3U5E9M8zTg5bQ39kGbub6ILKHzV6idCoHXhUrYJ5Tj0c2a+B/mZXH6RkBl9 UGu89FOJtrTwT55g8w5iejx23HG0u6xbHkN0hm7UbbQiQAGWc8XpQh5GA3p8zZxcXzD9 dHIDb4HrSr4CaMb2buNyY3sZU/dgfkGhEhjxJx/wEP566DkmYLR/navCNSNtGCtzLlOq AleQ== MIME-Version: 1.0 X-Received: by 10.50.131.196 with SMTP id oo4mr7417917igb.2.1428794477796; Sat, 11 Apr 2015 16:21:17 -0700 (PDT) In-Reply-To: <20150411230800.GA26817@cskk.homeip.net> References: <20150411230800.GA26817@cskk.homeip.net> Date: Sun, 12 Apr 2015 09:21:17 +1000 Subject: Re: try..except with empty exceptions From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 60 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1428794480 news.xs4all.nl 2831 [2001:888:2000:d::a6]:46080 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:88848 On Sun, Apr 12, 2015 at 9:08 AM, Cameron Simpson wrote: > 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. Fair enough. Do you know how often you actually catch stuff that wouldn't be caught by "except BaseException:"? I would guess it's pretty rare. In fact, you can find out. try: r = func(*a, **kw) except BaseException: self.exc_info = sys.exc_info except: self.exc_info = sys.exc_info emit_warning() else: self.result = r You could simply mandate that, from version X.Y of your Asynchron module onward, old-style classes must not be thrown. > 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. > I'm -0.1 on the idea myself. I consider "except:" succinct and evocative, > and prefer it to "except BaseException:". What you're looking at here is exactly why it's not spelled that way. The normal thing to do is NOT to catch absolutely everything, but to allow KeyboardInterrupt and SystemExit to continue to bubble up. That's spelled "except Exception". My whole point here is that the bare except catches too much for normal use, and therefore should _not_ be pleasing to the eye. The thing you should most often be doing is "except ValueError" or equivalent; forcing you to put at least _some_ name into the exception clause emphasizes that being specific is not the unusual case. ChrisA