在测试的一段代码,看起来有点像,我已经得到了一惊:引发错误不会阻止try-except子句执行吗?
if x:
try:
obj = look-for-item-with-id==x in a db
if obj is None:
# print debug message
raise NotFound('No item with this id')
return obj
except Exception, e:
raise Error(e.message)
我预计,如果没有项目,在一个数据库提供的ID(X),该NotFound异常会被抚养。但是,在获得if子句并打印调试消息后,它会转到except子句并引发异常(exc消息是Item not found ...)。有人可以这样善良,在这里启发我吗?
您正在引发'NotFound',捕获它,然后引发'Error'。你期望会发生什么? – tzaman
好吧,我现在明白了。加注并不是逃避条款,而是被抓住。耻辱在我:) – fanny