gpt4 book ai didi

block 内的 python 异常处理

转载 作者:太空宇宙 更新时间:2023-11-03 14:44:35 24 4
gpt4 key购买 nike

下面的代码是不是对python3的with语句和异常处理做错了什么?如果不是,编写预期输出的正确方法是什么?

from contextlib import contextmanager

@contextmanager
def test():
print("Hello")
yield
print("goodbye")

try:
with test():
print("inside test")
raise KeyError
except KeyError:
print("KeyError")
else:
print("else")
finally:
print("finally")

输出是

Hello
inside test
KeyError
finally

期望输出是:

Hello
inside test
goodbye
KeyError
finally

我相信other people类似地写,希望在处理文件过程中出现异常时关闭文件。

我的python3版本是:

Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.version)
3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609]

最佳答案

with 管理的 block 内的异常语句通过 generator.throw() 传播到您的生成器上下文管理器如图PEP 343: "Generator Decorator" ,这会在生成器暂停的地方引发异常。换句话说,您应该将 yield 包装在 try/except 或 try/finally 中:

@contextmanager
def test():
print("Hello")
try:
# The block of the with statement executes when the generator yields
yield

finally:
print("goodbye")

引用official documentation关于主题:

...If an unhandled exception occurs in the block, it is reraised inside the generator at the point where the yield occurred. Thus, you can use a tryexceptfinally statement to trap the error (if any), or ensure that some cleanup takes place. If an exception is trapped merely in order to log it or to perform some action (rather than to suppress it entirely), the generator must reraise that exception. Otherwise the generator context manager will indicate to the with statement that the exception has been handled, and execution will resume with the statement immediately following the with statement.

关于 block 内的 python 异常处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50715920/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com