ConnectionError handling problem
cs at zip.com.au
Fri Sep 25 01:25:38 CEST 2015
On 24Sep2015 12:38, Laura Creighton <lac at 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
>>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. :)
>>> r=requests.post(url, data=query_args)
>>> print "Connection error"
>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.
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 zip.com.au>
More information about the Python-list