Python handling specific error codes?

59,800

Solution 1

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.

Solution 2

On Unix platforms, at least, you can do the following.

import socket, errno
try:
    # Do something...
except socket.error as e:
    if e.errno == errno.ECONNREFUSED:
        # Handle the exception...
    else:
        raise

Before Python 2.6, use e.args[ 0 ] instead of e.errno.

Solution 3

This seems hard to do reliably/portably but perhaps something like:

import socket

try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(('localhost', 4167))
except socket.error, e:
    if 'Connection refused' in e:
        print '*** Connection refused ***'

which yields:

$ python socketexception.py 
*** Connection refused ***

Pretty yucky though.

Share:
59,800

Related videos on Youtube

AustinM
Author by

AustinM

Updated on July 09, 2022

Comments

  • AustinM
    AustinM almost 2 years

    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.

  • Yavor Angelov
    Yavor Angelov about 13 years
    Why would you say this is unreliable/not portable? What is "yucky" about this?
  • Thomas Wouters
    Thomas Wouters about 13 years
    Using e.errno instead of e.args[0] is usually preferred (for exceptions that use errnos).
  • Marc Abramowitz
    Marc Abramowitz about 13 years
    Because I am not 100% sure that the exception message on Windows would have "Connection refused" in it and not something similar but different like "Could not connect". Not sure if those error messages are standardize (e.g.: in POSIX) and besides not all platforms are necessarily POSIX-compliant.
  • jchl
    jchl about 13 years
    On OSX, ECONNREFUSED appears to be 61 not 111, so hard-coding the value 111 would be a bad idea for portability.
  • Utku Zihnioglu
    Utku Zihnioglu about 13 years
    You are absolutely right. I am sure that there are more cases like that.
  • jchl
    jchl about 13 years
    I thought that to begin with, but testing it out on my Mac it seemed that socket.error didn't have an errno member. It turns out that before Python 2.6, socket.error wasn't a subclass of IOError and so didn't have an errno member. But of course, before Python 2.6 the except t as e syntax wasn't valid either... I'll update my code.
  • sum1stolemyname
    sum1stolemyname almost 10 years
    (-1) WARNING: This will fail completely in non-english locales.
  • Marc Abramowitz
    Marc Abramowitz almost 10 years
    Yeah, that's a good point. This will only work in an English locale. All in all, this is a pretty terrible idea and you should use errno like in the above answers.
  • jtpereyda
    jtpereyda over 7 years
    It's important to have an else: raise, otherwise all other error codes will be silently ignored!
  • byxor
    byxor over 7 years
    Thanks. This resolved all visual warnings in PyCharm about errno not being a valid attribute of Exception, even though the code ran perfectly fine.
  • Anatoly Alekseev
    Anatoly Alekseev over 5 years
    @jtpereyda did you mean except: raise ?
  • jtpereyda
    jtpereyda over 5 years
    @AnatolyAlekseev no, I mean the same code with an else: raise tacked on the end. See @jchl answer for an example. By "all other error codes" I mean all other socket.error objects with errorcode != errno.ECONNREFUSED
  • Anatoly Alekseev
    Anatoly Alekseev over 5 years
    @jtpereyda this is weird then. I thought else: xxx is called when there were no exception inside try.. block. Docs for Python 3 confirm this. Was it different for python 2?
  • jtpereyda
    jtpereyda over 5 years
    @AnatolyAlekseev I mean an else that pairs with the if. So the else would be at the first indent level, within the except block.
  • Jerther
    Jerther over 5 years
    os.strerror(error.errno) will convert the error code to a message string. i.e.: os.strerror(104) returns 'Connection reset by peer'
  • MestreLion
    MestreLion about 3 years
    +1 for the honesty of saying "Pretty yucky, non-portable, locale-dependent, terrible idea, please don't use" :-)