返回::[wiki:self/PyIAQ Python 罕见问题集]
::-- ["huangyi"] [DateTime(2006-04-22T14:41:38Z)]
::-- ZoomQuiet [DateTime(2005-09-06T04:10:30Z)]
1. 问:finally子句里面代码是不是总是会被执行?
Q: The code in a finally clause will never fail to execute, right??
What never? Well, hardly ever. The code in a finally clause does get executed after the try clause whether or not there is an exception, and even if sys.exit is called. However, the finally clause will not execute if execution never gets to it. This would happen regardless of the value of choice in the following:
什么总是?当然,大多数时候是这样的。try子句后面的finally子句的代码确实不管是否发生异常都会被执行,甚至是调用了sys.exit也一样。但是,如果程序永远也不会运行到那里去,finally子句的代码就不会被执行。比如下面的例子中,不过choice的值是什么都会发生这种情况。
2. 反馈
谢谢参与!我们的原则就是积极主动,及时分享! -- ZoomQuiet