ConnectionError handling problem

shiva upreti katewinslet626 at gmail.com
Fri Sep 25 07:41:30 CEST 2015


On Thursday, September 24, 2015 at 4:09:04 PM UTC+5:30, Laura Creighton wrote:
> 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

Thank you. I didnt know about keyboard interrupt exception.
It means my code hangs without raising any exceptions, what should i do in this case? 


More information about the Python-list mailing list