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


Groups > comp.lang.python > #43216

Re: newbie question about confusing exception handling in urllib

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder7.xlned.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!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.008
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'else:': 0.03; 'skip:u 30': 0.07; 'clause': 0.09; 'try:': 0.09; 'subject:question': 0.10; 'blocks': 0.16; 'catch-all': 0.16; 'subject:exception': 0.16; 'subject:handling': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'basically': 0.19; 'pointed': 0.19; 'cheers,': 0.24; 'class.': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'raise': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'except': 0.35; 'received:google.com': 0.35; 'two': 0.37; 'to:addr :python-list': 0.38; 'rather': 0.38; 'to:addr:python.org': 0.39; 'catch': 0.60; 'ian': 0.60; 'needing': 0.65; 'nobody': 0.68; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:in-reply-to:references:from:date:message-id :subject:to:content-type; bh=3IN9irZkFqDFckiOpYFnPgsnwntOYVpyUJYf27kMQVg=; b=NdRQ6ESUC+/eUfJfwiGd04Y/gRYyrSjOZ+h1l1yZGk2qGsyHAVcPMDdj8P3C4dwAai 0ZZtQudFYhNPoB/UuErA+64FwrvSpo+Wo47yyDVqf/h4qt0v6QUPVHNbVEKPBm3SWt6S tX0gZ2y0LoyQed12FmUkWwMW5DfeHfDbpNHft6zX1l4bOcmWBuJRzXo3GS2kzqAA1IHD tblGQfchs1nJoCcITCQF25K36Ufqb3Xl4yEkzxdp9wpY2NHR0S+LgudHNkZlD/v8DbGs X0a4l8r0YyF5NY7fYxmoXyeWKaI1X3cBA7ua5RjivlEraHUTalWzxUfZenp8bNjRhQY+ vEQA==
X-Received by 10.68.125.227 with SMTP id mt3mr4006908pbb.153.1365534719595; Tue, 09 Apr 2013 12:11:59 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <1ae3261b-078d-4362-abff-ea4471addd6a@googlegroups.com>
References <1ae3261b-078d-4362-abff-ea4471addd6a@googlegroups.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Tue, 9 Apr 2013 13:11:18 -0600
Subject Re: newbie question about confusing exception handling in urllib
To Python <python-list@python.org>
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.378.1365534727.3114.python-list@python.org> (permalink)
Lines 28
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1365534727 news.xs4all.nl 2650 [2001:888:2000:d::a6]:51632
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:43216

Show key headers only | View raw


On Tue, Apr 9, 2013 at 5:41 AM,  <cabbar@gmail.com> wrote:
>         try:
>             response = urllib.request.urlopen(request)
>             content = response.read()
>         except BaseException as ue:
>             if (isinstance(ue, socket.timeout) or (hasattr(ue, "reason") and isinstance(ue.reason, socket.timeout)) or isinstance(ue, ConnectionResetError)):
>                 print("REQUEST TIMED OUT")

I'm surprised nobody has yet pointed out that you can catch multiple
specific exception types in the except clause rather than needing to
organize them under a catch-all base class.  These two code blocks are
basically equivalent:

try:
    do_stuff()
except BaseException as ue:
    if isinstance(ue, (socket.timeout, ConnectionResetError)):
        handle_it()
    else:
        raise

try:
    do_stuff()
except (socket.timeout, ConnectionResetError) as ue:
    handle_it()

Cheers,
Ian

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


Thread

newbie question about confusing exception handling in urllib cabbar@gmail.com - 2013-04-09 04:41 -0700
  Re: newbie question about confusing exception handling in urllib Peter Otten <__peter__@web.de> - 2013-04-09 14:19 +0200
  Re: newbie question about confusing exception handling in urllib cabbar@gmail.com - 2013-04-09 06:19 -0700
    Re: newbie question about confusing exception handling in urllib Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-04-09 15:05 +0000
      Re: newbie question about confusing exception handling in urllib Chris Angelico <rosuav@gmail.com> - 2013-04-10 02:23 +1000
      RE: newbie question about confusing exception handling in urllib "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2013-04-12 21:29 +0000
  Re: newbie question about confusing exception handling in urllib Terry Jan Reedy <tjreedy@udel.edu> - 2013-04-09 10:19 -0400
  Re: newbie question about confusing exception handling in urllib Ian Kelly <ian.g.kelly@gmail.com> - 2013-04-09 13:11 -0600

csiph-web