python with块内部的异常处理
python exception handling inside with block
以下代码是否对 with
语句和 python3 的异常处理有任何错误?如果不是,我的预期输出的正确写法是什么?
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 statement is propagated to your generator context manager through generator.throw()
as shown in 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 try…except…finally 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.
以下代码是否对 with
语句和 python3 的异常处理有任何错误?如果不是,我的预期输出的正确写法是什么?
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 statement is propagated to your generator context manager through generator.throw()
as shown in 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 try…except…finally 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.