【发布时间】:2020-10-13 03:13:55
【问题描述】:
class DemoException(Exception):
"""An exception type for the demonstration."""
def demo_exc_handling():
print('-> coroutine started')
while True:
try:
x = yield
except DemoException: # <1>
print('*** DemoException handled. Continuing...')
else: # <2>
print('-> coroutine received: {!r}'.format(x))
finally:
print('-> 1111111111coroutine ending')
raise RuntimeError('This line should never run.')
if __name__ == '__main__':
exc_coro = demo_exc_handling()
next(exc_coro)
exc_coro.send(11)
我得到以下输出:
-> coroutine started
-> coroutine received: 11
-> 1111111111coroutine ending
-> 1111111111coroutine ending
我想知道finally语句为什么会执行两次? 如果有任何帮助,我将不胜感激。
【问题讨论】: