【发布时间】:2013-12-28 06:22:21
【问题描述】:
我有一个 python 代码,我想引发异常:
def main():
try:
if testfile == 'testfile':
write_file(create_output())
print_file()
return 0
except:
return 1
即使 testfile != 'testfile' 函数不返回 1。为什么?
【问题讨论】:
-
也许不会引发异常。如果您想引发异常,请使用 ...
raise关键字:) 您的最后一条评论表明您想要而不是if...else构造不是try...except。 -
你首先想通过这段代码实现什么? (即,为什么这个问题被标记为unit-testing?)
标签: python unit-testing exception-handling