How can handle exception?
Alex Martelli
aleax at aleax.it
Sat Jan 5 05:14:41 EST 2002
A wrote:
> Hello,
> My program sometimes raise the exception
>
> socket.error: (10061, 'Connection refused')
>
> How can I handle this kind of exception?
You can execute the part that is raising this exception in the try clause
of a try/except statement:
import socket
...
try:
some_stuff()
some_other()
and_so_on()
except socket.error, err:
whatever()
The problem, of course, is what to put in the "whatever" -- but that's
something that only the application's author (that's you) can possibly
know. What DO you want to do when the server you're trying to contact is
refusing the connection? If you want to wait a while and retry a few
times, put the whole try/except in a loop, and break from the loop upon
success (e.g. in an else clause of the try/except) while letting the loop
repeat (possibly after some warning message to the user) if it enters the
except clause.
Alex
More information about the Python-list
mailing list