Alex Rivera | Logout

Python handling specific error codes?

Asked 2011-03-01T22:25:39.033
28

Hey I'm wondering how to handle specific error codes. For example, [Errno 111] Connection refused

I want to catch this specific error in the socket module and print something.

Edit
Report

1 Answer

41

If you want to get the error code, this seems to do the trick;

import errno

try:
    socket_connection()
except socket.error as error:
    if error.errno == errno.ECONNREFUSED:
        print(os.strerror(error.errno))
    else:
        raise

You can look up errno error codes.

answered 2011-03-01T22:40:16.423

Your Answer