Python:捕获特定异常
我有这样的代码(Python 2.5,GAE dev服务器): 所以我在最后一行设置了断点,在调试器下我看到:Python:捕获特定异常,python,google-app-engine,Python,Google App Engine,我有这样的代码(Python 2.5,GAE dev服务器): 所以我在最后一行设置了断点,在调试器下我看到: "exc" TokenUpgradeFailed: {'status': 403, 'body': 'html stripped', 'reason': 'Non 200 response on upgrade'} "type(exc)" type: <class 'gdata.service.TokenUpgradeFailed'> "exc is gdata.
"exc" TokenUpgradeFailed: {'status': 403, 'body': 'html stripped', 'reason': 'Non 200 response on upgrade'}
"type(exc)" type: <class 'gdata.service.TokenUpgradeFailed'>
"exc is gdata.service.TokenUpgradeFailed" bool: False
"exc.__class__" type: <class 'gdata.service.TokenUpgradeFailed'>
"isinstance(exc, gdata.service.TokenUpgradeFailed)" bool: False
"exc.__class__.__name__" str: TokenUpgradeFailed
“exc”令牌升级失败:{'status':403,'body':'html stripped','reason':'Non 200 response on upgrade'}
“类型(exc)”类型:
“exc是gdata.service.TokenUpgradeFailed”bool:False
“exc.\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu类”类型:
“isinstance(exc,gdata.service.TokenUpgradeFailed)”bool:False
“exc.\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
我在python异常处理中遗漏了什么?为什么isinstance(exc,gdata.service.TokenUpgradeFailed)为False?如果您的相对/绝对
import
语句不匹配,则可能发生此错误。如果存在不匹配,则可以在稍微不同的上下文中多次加载目标模块。通常这不是问题,但它确实会阻止来自不同加载模块的类进行相等比较(因此出现异常捕获问题)
错误可能还有其他原因,但我建议仔细检查代码,确保导入gdata.service
模块的所有内容都明确提到gdata
包。即使在gdata
包本身中,使用服务的每个模块都应该通过从gdata导入服务
显式地从包中导入,而不是通过相对导入:导入服务
"exc" TokenUpgradeFailed: {'status': 403, 'body': 'html stripped', 'reason': 'Non 200 response on upgrade'}
"type(exc)" type: <class 'gdata.service.TokenUpgradeFailed'>
"exc is gdata.service.TokenUpgradeFailed" bool: False
"exc.__class__" type: <class 'gdata.service.TokenUpgradeFailed'>
"isinstance(exc, gdata.service.TokenUpgradeFailed)" bool: False
"exc.__class__.__name__" str: TokenUpgradeFailed