Catching very specific exceptions
Peter Hansen
peter at engcorp.com
Sun Jan 22 14:00:42 EST 2006
Alex Martelli wrote:
> 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.
If it does not, or as a more generally applicable solution (though less
readable), one can use the args attribute:
except socket.error, e:
if e.args[0] == 10061:
...
elif e.args[0] == 10060:
...
args is just the tuple formed from the positional arguments passed to
the __init__() of the exception object when it is created, as if you did:
class Exception:
def __init__(self, *args):
self.args = args
-Peter
More information about the Python-list
mailing list