Path: csiph.com!eternal-september.org!feeder.eternal-september.org!border1.nntp.ams1.giganews.com!nntp.giganews.com!usenetcore.com!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'python,': 0.02; 'except:': 0.07; 'cc:addr:python-list': 0.09; '"connection': 0.16; '10:45': 0.16; 'catching),': 0.16; 'error"': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'subject:handling': 0.16; 'wrote:': 0.16; 'stick': 0.18; 'try:': 0.18; '(in': 0.18; '>>>': 0.20; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'exceptions': 0.22; 'lawrence': 0.22; 'sep': 0.22; 'subject:problem': 0.22; 'absolute': 0.23; 'header:In-Reply-To:1': 0.24; 'message-id:@mail.gmail.com': 0.27; "skip:' 10": 0.28; 'catching': 0.29; 'print': 0.30; 'code': 0.30; 'probably': 0.31; 'maybe': 0.33; 'getting': 0.33; 'except': 0.34; 'handle': 0.34; 'received:google.com': 0.35; 'ones': 0.35; 'should': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'agree': 0.37; '(with': 0.38; 'minimum': 0.38; 'anything': 0.38; 'mark': 0.40; 'your': 0.60; 'maximum': 0.61; 'to,': 0.63; '20,': 0.66; 'worth': 0.67; 'obvious': 0.76; 'chrisa': 0.84; 'thrown,': 0.84; 'to:none': 0.91 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=Hq6/arUGIu+KqUs1PIcpwfEN6jfME2Z93QIVMPMvVVg=; b=FoViFHjc8t1XML5foicB6Y/X4EQL2wUxM5CzeV1cV0pQUW4QlyYJx+CGKPsxT00GI3 Mvop+7o+eGTKVfFbIqtUqmtgCRIRUkoiBHWOH9D/pXKroauP/GNPCA5XqUsUQOERvcUl vYX4EpvCHiDEyLlp/fexeFqLkaJZJRC5namIbCxiD/bLj9BPm9+2SUhszxMJW82wVjYa eE+trSNfF4W2ZZdI9VfFaNxQ1ni1jTqxrIqKDH9281KtpTXqfSpb6Mei80EBEyU6AtyF 0H8LWoiiEZGwGz3vZlFHOog8cerI4aRN67Hz38A74Gv9uRzhHsOwEX7WrB4M1Q9eA7xR rn/A== MIME-Version: 1.0 X-Received: by 10.50.178.145 with SMTP id cy17mr6846125igc.92.1442755558461; Sun, 20 Sep 2015 06:25:58 -0700 (PDT) In-Reply-To: References: Date: Sun, 20 Sep 2015 23:25:58 +1000 Subject: Re: ConnectionError handling problem 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: 26 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1442755567 news.xs4all.nl 23748 [2001:888:2000:d::a6]:44578 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:96876 On Sun, Sep 20, 2015 at 10:45 PM, Jon Ribbens wrote: > On 2015-09-19, Mark Lawrence wrote: >> On 19/09/2015 07:13, shiva upreti wrote: >>> try: >>> r=requests.post(url, data=query_args) >>> except: >>> print "Connection error" >> >> Never use a bare except in Python, always handle the bare minimum number >> of exceptions that you need to, in this case your ConnectionError. > > While I entirely agree with the principle of being specific in what > exceptions you are catching (with the absolute maximum being > 'Exception'), it is often not obvious which ones you need to specify. > The code above probably actually needs to catch EnvironmentError if > it is intended to intercept all network problems. General principle: If you don't know what you should be catching, _catch nothing_. Anything that happens will get printed to the console. Then when you find that something's getting thrown, you check out what its name is, and maybe what its superclasses are (in case there's a broader one worth catching), and only THEN do you stick in a try/except. ChrisA