【发布时间】:2020-02-22 00:37:17
【问题描述】:
以下代码将JUST KIDDING 打印到控制台。为什么不打印“HIGH FIVE ME!”?
def f():
print("`f` is being called")
try:
raise ValueError()
raise AttributeError()
raise type("Hell", (Exception,), dict())
except BaseException as exc:
print("we are in the except block")
# WHY IS "HIGH FIVE ME!" NOT RETURNED?
return "HIGH FIVE ME!"
finally:
return "JUST KIDDING"
print(f())
如果这是所需的行为,是否可以实现类似以下伪代码的内容?
If:
finally clause, `fin` exists
and:
there exists except block `exc` in `fin` such that:
there exists a return statement in `exc`
then:
raise terrible horrible bad ugly exception
【问题讨论】:
-
我不遵循你的伪代码。能不能少写点伪代码多写点代码?
-
嘿牙签海葵,如果您愿意编辑您的问题以澄清伪代码,我很乐意修改我的答案。我想我设法回答了其余的问题。还是我没有?
标签: python python-3.x exception try-catch