有没有一种方法可以在上下文管理器的方法中捕获异常,__enter__
而无需将整个with
块包装在 a中try
?
class TstContx(object):
def __enter__(self):
raise Exception("I'd like to catch this exception")
def __exit__(self, e_typ, e_val, trcbak):
pass
with TstContx():
raise Exception("I don't want to catch this exception")
pass
我知道我可以在__enter__()
自身内部捕获异常,但是我可以从包含该with
语句的函数中访问该错误吗?
从表面上看,在上下文管理器中捕获异常__enter__()
的问题似乎是一回事,但这个问题实际上是关于确保__exit__
调用它,而不是与语句所包含__enter__
的块不同地处理代码。with
...显然动机应该更清楚。该with
声明正在为一个完全自动化的过程设置一些日志记录。如果程序在日志设置之前失败,那么我不能依靠日志来通知我,所以我必须做一些特别的事情。而且我宁愿在不必添加更多缩进的情况下实现效果,如下所示:
try:
with TstContx():
try:
print "Do something"
except Exception:
print "Here's where I would handle exception generated within the body of the with statement"
except Exception:
print "Here's where I'd handle an exception that occurs in __enter__ (and I suppose also __exit__)"
使用两个try
块的另一个缺点是处理异常__enter__
的代码在块的后续主体中处理异常的代码之后with
。