Catching very specific exceptions

Alex Martelli aleax at mail.comcast.net
Sun Jan 22 11:50:23 EST 2006


Harlin Seritt <harlinseritt at yahoo.com> wrote:

> except socket.error:
>     code goes here...
> 
> Of course, though, these are two separate error messages under the same
> error handler though. I've tried:
> 
> except socket.error, (10061, 'Connection refused'):
> and
> except (socket.error, 10061, 'Connection refused'):
> 
> to no avail. What do I need to do to catch each one specifically?

what I've done in such situations is

except socket.error, e:
  if e.errno == 10061:
     ...
  elif e.errno == 10060:
     ...
  else:
    raise

Not sure the code in a socket.error has attributename 'errno', but I
hope you get the general idea.


Alex



More information about the Python-list mailing list