楼主:
ResolaQQ (ResolaQQ)
2016-01-19 19:09:15class A:
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
raise Exception('A')
在 __exit__ 一定会丢出 Exception 的情况下,底下这段的结果在预期之中
>>>> with A():
.... pass
....
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "<stdin>", line 5, in __exit__
Exception: A
但底下这段就有问题了,先丢出的 Exception 被后丢出的覆蓋
>>>> with A():
.... raise Exception('B')
....
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "<stdin>", line 5, in __exit__
Exception: A
请问该怎么写才能比较好处理这种情况?
我遇上 A, B, C 的 __exit__ 都有可能丢出 Exception
程式本体也有可能丢出 Exception,像这样
>>> with A(), B(), C():
... raise Exception('D')
...
结果一团乱,根本不知道要怎么处理...