Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #96876
| References | <c56e680b-2e04-4fc6-ba57-5ec64459bce9@googlegroups.com> <mailman.24.1442679607.21674.python-list@python.org> <slrnmvtanf.48q.jon+usenet@frosty.unequivocal.co.uk> |
|---|---|
| Date | 2015-09-20 23:25 +1000 |
| Subject | Re: ConnectionError handling problem |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.38.1442755567.21674.python-list@python.org> (permalink) |
On Sun, Sep 20, 2015 at 10:45 PM, Jon Ribbens <jon+usenet@unequivocal.co.uk> wrote: > On 2015-09-19, Mark Lawrence <breamoreboy@yahoo.co.uk> 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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
ConnectionError handling problem shiva upreti <katewinslet626@gmail.com> - 2015-09-18 23:13 -0700
Re: ConnectionError handling problem Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-09-19 17:18 +0100
Re: ConnectionError handling problem Jon Ribbens <jon+usenet@unequivocal.co.uk> - 2015-09-20 12:45 +0000
Re: ConnectionError handling problem Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-09-20 14:13 +0100
Re: ConnectionError handling problem Jon Ribbens <jon+usenet@unequivocal.co.uk> - 2015-09-20 13:28 +0000
Re: ConnectionError handling problem Chris Angelico <rosuav@gmail.com> - 2015-09-20 23:25 +1000
Re: ConnectionError handling problem Jon Ribbens <jon+usenet@unequivocal.co.uk> - 2015-09-20 13:33 +0000
Re: ConnectionError handling problem Laura Creighton <lac@openend.se> - 2015-09-20 16:40 +0200
Re: ConnectionError handling problem shiva upreti <katewinslet626@gmail.com> - 2015-09-23 19:49 -0700
Re: ConnectionError handling problem Laura Creighton <lac@openend.se> - 2015-09-24 12:38 +0200
Re: ConnectionError handling problem shiva upreti <katewinslet626@gmail.com> - 2015-09-24 20:52 -0700
Re: ConnectionError handling problem shiva upreti <katewinslet626@gmail.com> - 2015-09-24 22:41 -0700
Re: ConnectionError handling problem shiva upreti <katewinslet626@gmail.com> - 2015-09-24 22:42 -0700
csiph-web