ConnectionError handling problem

Cameron Simpson cs at
Fri Sep 25 01:25:38 CEST 2015

On 24Sep2015 12:38, Laura Creighton <lac at> 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:
>>>, 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:, 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.

Cameron Simpson <cs at>

More information about the Python-list mailing list