Python handling specific error codes?

Python handling specific error codes?



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


[Errno 111] Connection refused



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




4 Answers
4



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.


errno





On OSX, ECONNREFUSED appears to be 61 not 111, so hard-coding the value 111 would be a bad idea for portability.
– jchl
Mar 1 '11 at 22:46


ECONNREFUSED





You are absolutely right. I am sure that there are more cases like that.
– Utku Zihnioglu
Mar 1 '11 at 22:48





It's important to have an else: raise, otherwise all other error codes will be silently ignored!
– jtpereyda
Sep 3 '16 at 22:56


else: raise





@AnatolyAlekseev I mean an else that pairs with the if. So the else would be at the first indent level, within the except block.
– jtpereyda
Aug 29 at 3:49


else


if


except





os.strerror(error.errno) will convert the error code to a message string. i.e.: os.strerror(104) returns 'Connection reset by peer'
– Jerther
Nov 14 at 14:31


os.strerror(error.errno)


os.strerror(104)



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.


e.args[ 0 ]


e.errno





Using e.errno instead of e.args[0] is usually preferred (for exceptions that use errnos).
– Thomas Wouters
Mar 1 '11 at 22:44


e.errno


e.args[0]





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.
– jchl
Mar 1 '11 at 23:07



socket.error


errno


socket.error


IOError


errno


except t as e



I'm developing on Windows and found myself in the same predicament. But the error message always contains the error number. Using that information I just convert the exception to a string str(Exception), convert the error code I wanna check for to a string str(socket.errno.ERRORX) and check if the error code is in the exception.


str(Exception)


str(socket.errno.ERRORX)



Example for a connection reset exception:


except Exception as errorMessage:
if str(socket.errno.ECONNRESET) in str(errorMessage):
print("Connection reset")
#etc...



This avoids locale specific solutions but is still not platform independent unfortunately.



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.





Why would you say this is unreliable/not portable? What is "yucky" about this?
– Nick Presta
Mar 1 '11 at 22:39





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.
– Marc Abramowitz
Mar 1 '11 at 22:44





(-1) WARNING: This will fail completely in non-english locales.
– sum1stolemyname
Jun 12 '14 at 19:29






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.
– Marc Abramowitz
Jun 13 '14 at 23:28


errno



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)