471,357 Members | 1,046 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,357 software developers and data experts.

How to catch error

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
5 10365
dazzler
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
dazzler
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
dazzler
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
dazzler
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
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.

Similar topics

11 posts views Thread by kaeli | last post: by
7 posts views Thread by Arjen | last post: by
23 posts views Thread by VB Programmer | last post: by
3 posts views Thread by will | last post: by
32 posts views Thread by cj | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.