Catching very specific exceptions

Harlin Seritt harlinseritt at
Sun Jan 22 10:32:03 EST 2006

I am running the following code:

import socket

host = ''
port = 10001

conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
conn.connect((host, port))

When conn.connect() is run, there can be two different exceptions:

socket.error: (10061, 'Connection refused')
socket.error: (10060, 'Operation timed out')

How can I set up exceptions for each of these differently? Normally I
would use:

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'):
except (socket.error, 10061, 'Connection refused'):

to no avail. What do I need to do to catch each one specifically?


Harlin Seritt

More information about the Python-list mailing list