python上下文 - 经理装饰器掩盖我的例外?
按照 ..
在下面的合成代码中:
import sys
from contextlib import contextmanager, nullcontext
class MyException(Exception):
def __init__(self, *args: object) -> None:
super().__init__(*args)
@contextmanager
def annotate():
try:
yield None
finally:
return
def annotate2():
return nullcontext()
def main():
try:
with annotate():
raise MyException()
except MyException:
print("Good, Caught exception")
sys.exit(0)
print("How did I get here ???")
sys.exit(1)
if __name__ == '__main__':
main()
未捕获例外。 但是,如果我将其更改
with annotate2():
raise MyException()
为预期的。 我的问题是,我的上下文管理员怎么了?
@contextmanager
def annotate():
try:
yield None
finally:
return
(用Python 3.10.4测试)
谢谢
Boaz
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
谢谢,找到了答案
https://docs.python.org3/教程/errors.html#定义清洁 - 上的actions
Thanks, found the answer in
https://docs.python.org/3/tutorial/errors.html#defining-clean-up-actions