How to catch error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dazzler
    New Member
    • Nov 2007
    • 75

    How to catch error

    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:

    [code=python]
    try:
    data = connection.recv (8048)
    except (error):
    print error
    [/code]
    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:

    [code=python]
    import errno
    ...

    try:
    data = connection.recv (8048)
    except, (error,message) :
    print error
    if error == errno.WSAEWOULD BLOCK:
    print "10035 error found, doing something..."
    [/code]

    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.WSAEWOULD BLOCK (that's the (windows) error 10035)

    could anyone help me? =)
  • dazzler
    New Member
    • Nov 2007
    • 75

    #2
    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...

    Comment

    • dazzler
      New Member
      • Nov 2007
      • 75

      #3
      Originally posted by dazzler
      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 =(

      Comment

      • dazzler
        New Member
        • Nov 2007
        • 75

        #4
        [code=python]
        try:
        connection, address = s.accept()
        data = connection.recv (8048)
        except socket.error:
        [/code]
        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 =/

        Comment

        • dazzler
          New Member
          • Nov 2007
          • 75

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

          [code=python]
          try:
          connection, address = s.accept()
          data = connection.recv (8048)

          except Exception, errorcode:
          if errorcode[0] == "timed out":
          print "time out, doing something..."
          elif errorcode[0] == 10035:
          print "error 10035, doing something..."
          [/code]

          Comment

          • factorion
            New Member
            • Jan 2008
            • 2

            #6
            [CODE="python"]try:
            connection, address = s.accept()
            data = connection.recv (8048)

            except socket.timeout:
            print "time out, doing something..."
            except Exception, errorcode:
            if errorcode[0] == 10035:
            print "error 10035, doing something..."[/CODE]

            I don't know if that will help you any, posting two months later, but that should be how to handle multiple exceptions.

            Comment

            Working...