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: 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 Date: Tue, 9 Apr 2013 13:11:18 -0600 Subject: Re: newbie question about confusing exception handling in urllib To: Python 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Tue, Apr 9, 2013 at 5:41 AM, 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