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

References <1ae3261b-078d-4362-abff-ea4471addd6a@googlegroups.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2013-04-09 13:11 -0600
Subject Re: newbie question about confusing exception handling in urllib
Newsgroups comp.lang.python
Message-ID <mailman.378.1365534727.3114.python-list@python.org> (permalink)

Show all headers | 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