Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #97107
| Date | 2015-09-25 09:25 +1000 |
|---|---|
| From | Cameron Simpson <cs@zip.com.au> |
| Subject | Re: ConnectionError handling problem |
| References | <201509241038.t8OAcT1Z013215@fido.openend.se> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.157.1443140162.28679.python-list@python.org> (permalink) |
On 24Sep2015 12:38, Laura Creighton <lac@openend.se> wrote:
>In a message of Wed, 23 Sep 2015 19:49:17 -0700, shiva upreti writes:
>>If my script hangs because of the reasons you mentioned above, why doesnt it
>>catch ConnectionError?
>>My script stops for a while and when I press CTRL+C, it shows ConnectionError without terminating the process, and the script resumes from where it left off.
>
>This is exactly what you asked it to do. :)
>
>>> try:
>>> r=requests.post(url, data=query_args)
>>> except:
>>> print "Connection error"
>>> time.sleep(30)
>>> continue
>
>try to do something until you get an Exception. Since that is a
>naked except, absolutely any Exception will do. That you
>print out 'Connection error' doesn't mean that you are only
>catching exceptions raised by trying to send something to the
>other end ... any Exception will do.
[... snip ...]
Since nobody has offered this advice, let me:
Firstly, as already remarked bare excepts are overkill - they catch all sorts
of things you shouldn't be handling.
That said, if you _don't know_ what exceptions are going to boils out of new
code, this is one way to see them all. However, you are catching _everything_
but _not_ finding out what happened.
Try this:
try:
r=requests.post(url, data=query_args)
except Exception as e:
print "Exception:", e
That will at least _tell_ you what happened. You code is blithely presuming
Connection Error and telling you that, but it is usually a lie.
Once you have characterised what exceptions you get, and which particular ones
to handle, then modify your code to catch _only_ those and perform the specific
suitable actions, and let the rest escape.
Cheers,
Cameron Simpson <cs@zip.com.au>
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Re: ConnectionError handling problem Cameron Simpson <cs@zip.com.au> - 2015-09-25 09:25 +1000
Re: ConnectionError handling problem shiva upreti <katewinslet626@gmail.com> - 2015-09-24 20:57 -0700
Re: ConnectionError handling problem Cameron Simpson <cs@zip.com.au> - 2015-09-25 14:59 +1000
Re: ConnectionError handling problem shiva upreti <katewinslet626@gmail.com> - 2015-09-24 22:46 -0700
Re: ConnectionError handling problem Cameron Simpson <cs@zip.com.au> - 2015-09-25 17:24 +1000
Re: ConnectionError handling problem shiva upreti <katewinslet626@gmail.com> - 2015-09-29 23:04 -0700
Re: ConnectionError handling problem Cameron Simpson <cs@zip.com.au> - 2015-10-01 07:30 +1000
Re: ConnectionError handling problem Laura Creighton <lac@openend.se> - 2015-09-30 23:56 +0200
csiph-web