blueFast
blueFast

Reputation: 44371

How to get the instance type of an Exception?

I have this code:

try:
    self.client.post(url, data, self.cookies, headers, auth, jsonrpc)
    self.status  = self.client.status
    self.mytime  = self.client.time
    self.text    = self.client.text
    self.length  = len(self.text)
except urllib2.URLError, error:
    print error
    self.exception = True
    self.urrlib2   = True
    if isinstance(error.reason, socket.timeout):
        self.timeout = True

But sometimes I get exceptions printing out like this:

URLError in POST > reason=The read operation timed out > <urlopen error The read operation timed out>

These are handled by the except urllib2.URLError. They should pass the if isinstance(error.reason, socket.timeout) test, but they do not.

So I would like to know what instance this exception is. How can I do this?

Upvotes: 2

Views: 1679

Answers (2)

ATOzTOA
ATOzTOA

Reputation: 35950

Use this:

import sys

try:
    self.client.post(url, data, self.cookies, headers, auth, jsonrpc)
    self.status  = self.client.status
    self.mytime  = self.client.time
    self.text    = self.client.text
    self.length  = len(self.text)
except urllib2.URLError, error:
    print error
    self.exception = True
    self.urrlib2   = True

    errno, errstr = sys.exc_info()[:2]
    if errno == socket.timeout:
        print "There was a timeout"
        self.timeout = True

Upvotes: 0

Martijn Pieters
Martijn Pieters

Reputation: 1121884

The type() function returns the type of an object.

You could use print type(error.reason) to diagnose what type of object reason is in this case.

Upvotes: 1

Related Questions