简体   繁体   中英

What is the purpose of “msg” in this try-except block?

I was reading the Python socket programming tutorial, and I found this except block in the program:

except socket.error, msg:
    print 'Failed to create socket. Error code: ' + str(msg[0]) + ' , Error message : ' + msg[1]
    sys.exit();

What exactly is "msg" referencing (I know it is referencing the error message); but what IS it and where does it pull it from?

Sorry if this question is worded poorly. I'm not certain what exactly I should be asking.

msg is the actual exception object that is being caught. For example:

try:
    x = Exception()
    raise x
except Exception, msg:
    assert x is msg

The modern way of writing that statement would be

except socket.error as msg

msg is the explanation of the error !

This exception is raised for socket-related errors . The accompanying value is either a string telling what went wrong or a pair (errno, string) representing an error returned by a system call, similar to the value accompanying os.error . See the module errno , which contains names for the error codes defined by the underlying operating system.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM