我有这样的代码:
try:
return make_success_result()
except FirstException:
handle_first_exception()
return make_error_result()
except SecondException:
handle_second_exception()
return make_error_result()
我想知道有什么办法可以做到:
^{pr2}$因此,代码按以下顺序之一执行:
try > else > finally
try > except > ???? > finally
EDIT: my point here is that
????
block should execute right after ANY of theexcept
blocks, meaning that it's an addition to error handling, not a substitution.
您可以:
在这种情况下,我要做的是在出现异常时设置一个布尔值,如下所示:
这应该符合您的需要,这是IMO将所有解决方案组合成最易读的代码结构的最干净的方法,这将是最容易调试的。在
你可以这样做:
但这是可怕的,我不会建议你没有大量的评论来说明为什么。。在
更新:除了内部try块中有趣的异常之外,您不需要捕获任何内容。代码已更新。在
UPDATE2:根据操作说明,
common
应该在引发有趣的异常时执行。代码已更新。@马特泰勒的版本绝对是最好的选择;-)相关问题 更多 >
编程相关推荐