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


Groups > comp.lang.python > #95162

Re: except block isn't catching exception

Path csiph.com!eternal-september.org!feeder.eternal-september.org!border1.nntp.ams1.giganews.com!nntp.giganews.com!bcyclone05.am1.xlned.com!bcyclone05.am1.xlned.com!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
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; 'handler': 0.04; '"""': 0.05; 'clause': 0.07; 'raises': 0.07; 'reason,': 0.07; 'type,': 0.07; 'wrapped': 0.07; 'block.': 0.09; 'exception,': 0.09; 'mentions': 0.09; 'exception': 0.13; 'stack': 0.13; 'canceled': 0.16; 'executed,': 0.16; 'statement.': 0.16; 'subject:exception': 0.16; 'then?': 0.16; 'wrote:': 0.16; 'case.': 0.18; 'language': 0.19; '2015': 0.20; 'aug': 0.20; 'seems': 0.23; 'header:In-Reply- To:1': 0.24; 'header': 0.24; 'chris': 0.26; 'fri,': 0.27; 'message-id:@mail.gmail.com': 0.27; '(it': 0.29; 'blocking': 0.29; 'starts': 0.29; 'there.': 0.30; 'code': 0.30; 'another': 0.32; "can't": 0.32; 'statement': 0.32; 'though,': 0.32; 'raised': 0.33; 'covered': 0.34; 'except': 0.34; 'received:google.com': 0.35; "isn't": 0.35; 'but': 0.36; 'possible.': 0.36; 'evaluation': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'there,': 0.37; 'why': 0.39; 'whatever': 0.39; 'does': 0.39; "didn't": 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'your': 0.60; 'entire': 0.61; 'statement,': 0.66; 'here': 0.66; 'finally': 0.70; 'interrupted': 0.76; 'expect.': 0.84; 'to:name:python': 0.84; 'played': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=pkPzXtVRfkBQKQfmKNE9T66kXcgqhP/xdxA3BCrWXrI=; b=1Dvn74Gc50u8Q0lC4rz5bk+MY0f5aXH3AP0yo56vibzAt+tvtOJs9KOzN2+1dT/qLh yiHH13ssd5FGzLjRifmhF0/2SeuQziZy05iaHgG01dJWqTBFlWU4GHEscvluKslNFKv3 TtWL7G0HV0X6JftHaUaIKeE8s1LB9utSN8X2BwyiJnO11emQZn0fVle3fVDfYBtZtjIw IkTRRm/GjJ24n6nuDFDzGxDqYeK9U74kGPM7dVgteZRjf0CRt1VX5eGRfL/eZuQyrb95 IMJ5YnrZxrSXwctaHL9aZY71kLwQAgACeKJ7t7HKM2VMUY1hTwozKJ/naWpUE2/9In5l SzWA==
X-Received by 10.50.111.164 with SMTP id ij4mr1897396igb.70.1439017042949; Fri, 07 Aug 2015 23:57:22 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <CAPTjJmqVQ2xftm7rxg+co0bTyafU_iQaEMu9gPgD-39z1qCa_w@mail.gmail.com>
References <48587161-1bd6-47e4-b2c9-7a37c60a59e5@googlegroups.com> <mailman.1286.1438908366.3674.python-list@python.org> <c0684935-c807-4d98-9a04-8c2f7986169d@googlegroups.com> <CAPTjJmqVQ2xftm7rxg+co0bTyafU_iQaEMu9gPgD-39z1qCa_w@mail.gmail.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Sat, 8 Aug 2015 00:56:43 -0600
Subject Re: except block isn't catching exception
To Python <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 <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.1330.1439017052.3674.python-list@python.org> (permalink)
Lines 33
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1439017052 news.xs4all.nl 2966 [2001:888:2000:d::a6]:36309
X-Complaints-To abuse@xs4all.nl
X-Received-Bytes 5211
X-Received-Body-CRC 3032646894
Xref csiph.com comp.lang.python:95162

Show key headers only | View raw


On Fri, Aug 7, 2015 at 8:44 PM, Chris Angelico <rosuav@gmail.com> wrote:
> The exception isn't happening inside sock.accept(), as I explained. So
> you can't catch it there.

Where does the exception happen then? Your explanation only covered
why the blocking call cannot be interrupted by it, not why the
exception isn't simply raised when the blocking call finishes.

I played around with this and found that if the try...except chain is
wrapped in another outer try statement, then the KeyboardInterrupt
exception does get caught by the outer exception handler as one might
expect. For the inner try statement though, neither any except block
nor the else block is executed, just the finally block. I didn't know
until now that was even possible.

The language reference mentions this in regard to the try statement:

"""
If the evaluation of an expression in the header of an except clause
raises an exception, the original search for a handler is canceled and
a search starts for the new exception in the surrounding code and on
the call stack (it is treated as if the entire try statement raised
the exception).
"""

So my theory as to what's going on here is that sock.accept raises a
socket.timeout exception, but then the KeyboardInterrupt is raised
before the first except block even begins to check the exception type,
and so it's treated as if the "entire try statement" raised
KeyboardInterrupt. Hence it can't be caught there, but only in an
outer try statement.

Whatever the reason, it definitely seems to be an interesting corner case.

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


Thread

except block isn't catching exception sohcahtoa82@gmail.com - 2015-08-06 17:34 -0700
  Re: except block isn't catching exception Chris Angelico <rosuav@gmail.com> - 2015-08-07 10:46 +1000
    Re: except block isn't catching exception sohcahtoa82@gmail.com - 2015-08-07 10:16 -0700
      Re: except block isn't catching exception Chris Angelico <rosuav@gmail.com> - 2015-08-08 12:44 +1000
      Re: except block isn't catching exception Ian Kelly <ian.g.kelly@gmail.com> - 2015-08-08 00:56 -0600
      Re: except block isn't catching exception Chris Angelico <rosuav@gmail.com> - 2015-08-08 17:08 +1000

csiph-web