Path: csiph.com!news.mixmin.net!weretis.net!feeder1.news.weretis.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'from:addr:yahoo.co.uk': 0.05; 'except:': 0.07; 'aliases': 0.09; 'oserror': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.10; 'exception': 0.13; '"connection': 0.16; '3.3,': 0.16; 'error"': 0.16; 'finer': 0.16; 'hierarchy.': 0.16; 'rationale': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subclasses,': 0.16; 'subject:handling': 0.16; 'wrote:': 0.16; 'try:': 0.18; 'language': 0.19; '>>>': 0.20; 'exceptions': 0.22; 'lawrence': 0.22; 'subject:problem': 0.22; 'absolute': 0.23; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; "skip:' 10": 0.28; 'catching': 0.29; 'pep': 0.29; 'print': 0.30; 'code': 0.30; "i'd": 0.31; 'probably': 0.31; '"the': 0.32; 'language.': 0.32; 'doubt': 0.33; 'except': 0.34; 'handle': 0.34; 'previous': 0.34; 'behind': 0.35; 'ones': 0.35; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'agree': 0.37; 'received:org': 0.37; 'thought': 0.37; 'starting': 0.37; '(with': 0.38; 'minimum': 0.38; 'to:addr:python.org': 0.40; 'mark': 0.40; 'your': 0.60; 'maximum': 0.61; 'charset:windows-1252': 0.62; 'more': 0.63; 'to,': 0.63; 'our': 0.64; 'obvious': 0.76; 'pythonistas,': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Mark Lawrence Subject: Re: ConnectionError handling problem Date: Sun, 20 Sep 2015 14:13:21 +0100 References: Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 195.147.66.69 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.2.0 In-Reply-To: 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: 31 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1442754847 news.xs4all.nl 23736 [2001:888:2000:d::a6]:39131 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:96874 On 20/09/2015 13:45, 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. > I doubt it, as from the docs "The following exceptions are kept for compatibility with previous versions; starting from Python 3.3, they are aliases of OSError.". EnvironmentError is one of those listed. I'd have thought it far more likely that you'd want to catch one or more of the OSError subclasses, as finer grained exceptions was part of the rationale behind PEP 3151 -- Reworking the OS and IO exception hierarchy. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence