【发布时间】:2012-12-30 13:10:41
【问题描述】:
我有这个代码:
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
但有时我会像这样打印出异常:
URLError in POST > reason=The read operation timed out > <urlopen error The read operation timed out>
这些由except urllib2.URLError 处理。他们应该通过if isinstance(error.reason, socket.timeout) 测试,但他们没有。
所以我想知道instance 这个异常是什么。我该怎么做?
【问题讨论】:
-
type()为您提供对象类型或类的字符串表示形式。 -
所以你的意思是我应该在这里做
type(error)?我要修改我的代码,异常需要几个小时才会出现……还是type(error.reason)? -
print type(error.reason)将帮助您诊断它是什么类型;没有太多答案,因此只是评论。 -
为什么不呢!?这是的答案(如果它有效)。所以我会测试它并接受它作为答案......
-
Erm,不是字符串表示,是类型对象。抱歉,需要多睡一会儿。