I have a socket-connection going on and I wanna improve the exception handling and Im stuck. Whenever I use the socket.connect(server_address) function with an invalid argument the program stops, but doesnt seem to throw any exceptions. Heres my code
import socket import sys import struct class ARToolkit(): def __init__(self): self.x = 0 self.y = 0 self.z = 0 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.logging = False def connect(self,server_address): try: self.sock.connect(server_address) except socket.error, msg: print "Couldnt connect with the socket-server: %s\n terminating program" % msg sys.exit(1) def initiate(self): self.sock.send("start_logging") def log(self): self.logging = True buf = self.sock.recv(6000) if len(buf)>0: nbuf = buf[len(buf)-12:len(buf)] self.x, self.y, self.z = struct.unpack("
The class maybe looks a bit wierd but its used for receiving coordinates from another computer running ARToolKit. Anyway, the issue is at the function connect()
:
def connect(self,server_address): try: self.sock.connect(server_address) except socket.error, msg: print "Couldnt connect with the socket-server: %s\n terminating program" % msg sys.exit(1)
If I call that function with a random IP-address and portnumber the whole program just stops up at the line:
self.sock.connect(server_address)
The documentation I've read states that in case of an error it will throw a socket.error-exception. I've also tried with just:
except Exception, msg:
This, if I'm not mistaken, will catch any exceptions, and still it yields no result. I would be very grateful for a helping hand. Also, is it okay to exit programs using sys.exit when an unwanted exception occurs?
Thank you