ConnectionError handling problem
Laura Creighton
lac at openend.se
Thu Sep 24 06:38:29 EDT 2015
In a message of Wed, 23 Sep 2015 19:49:17 -0700, shiva upreti writes:
>Hi
>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.
So what happens when you press Control-C?
You get a KeyboardInterrupt exception! :)
see: https://docs.python.org/2/library/exceptions.html
And you are catching it :)
And when you catch it you print Connection error and keep on
trying.
This is why people were telling you that naked try:except: pairs,
are rarely what you want. You didn't want to catch control-c but
you caught it anyway.
Laura
More information about the Python-list
mailing list