By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,222 Members | 2,374 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,222 IT Pros & Developers. It's quick & easy.

How to catch error

P: 75
I have this one code, where I have to catch a specific error code. (I'm using win xp)

data = connection.recv(8048)

without any error catching, I get error message:
"error: (10035, "The socket operation could not complete without blocking")
I need to identify this error message 10035 in my code but dont know how to do it.

I tried:

Expand|Select|Wrap|Line Numbers
  1. try:
  2.    data = connection.recv(8048)
  3. except (error):
  4.    print error
  5.  
it now prints <class 'socket error'> , and I want it to print that 10035error (and then handle it ofcourse)

I get somewhere the following code, but this doesn't work either:

Expand|Select|Wrap|Line Numbers
  1. import errno
  2. ...
  3.  
  4. try:
  5.    data = connection.recv(8048)
  6. except, (error,message):
  7.    print error
  8.    if error == errno.WSAEWOULDBLOCK:
  9.       print "10035 error found, doing something..."
  10.  
I don't understand that "," mark after the except, so the code gives me syntax error right away because of that... but in the code the error message should be a tuplet which I save to these variables and then see if the error code matches errno.WSAEWOULDBLOCK (that's the (windows) error 10035)

could anyone help me? =)
Nov 28 '07 #1
Share this Question
Share on Google+
5 Replies


P: 75
argh, I already found out the solution ;-D

it should be:
except Exception, (error, message):
not:
except, (error, message):

well, maybe this message will help someone else too who's having problems catching specific error codes...
Nov 28 '07 #2

P: 75
argh, I already found out the solution ;-D
well, it isn't working so well after all =D

because there is also other error which must be caught

except Exception, (error, message):
handles all exception, I want it to handle only this "10035 error" because when there happends other exception (socket timeout) I got this error:
....
Traceback (most recent call last):
....
except Exception, (error, message):
ValueError: need more than 1 value to unpack

so the expection is wrongly formatted to this other expection, uhm =(
Nov 28 '07 #3

P: 75
Expand|Select|Wrap|Line Numbers
  1. try:
  2.    connection, address = s.accept()
  3.    data = connection.recv(8048)
  4. except socket.error:
  5.  
this doesn't work, I get:
"AttributeError: '_socketobject' object has no attribute 'error' "

there is socket.timeout exception in socket module but I don't know how to use it =/
Nov 28 '07 #4

P: 75
ok, I solved this, but I think that there's also better solution, I dont want to compare that "timed out" string ;)

Expand|Select|Wrap|Line Numbers
  1. try:
  2.    connection, address = s.accept()
  3.    data = connection.recv(8048)
  4.  
  5. except Exception, errorcode:
  6.    if errorcode[0] == "timed out":
  7.       print "time out, doing something..."
  8.    elif errorcode[0] == 10035:
  9.       print "error 10035, doing something..."
  10.  
Nov 28 '07 #5

P: 2
Expand|Select|Wrap|Line Numbers
  1. try:
  2.    connection, address = s.accept()
  3.    data = connection.recv(8048)
  4.  
  5. except socket.timeout:
  6.    print "time out, doing something..."
  7. except Exception, errorcode:
  8.    if errorcode[0] == 10035:
  9.       print "error 10035, doing something..."
I don't know if that will help you any, posting two months later, but that should be how to handle multiple exceptions.
Jan 17 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.