python - Try-Except-Finally 语句的过时书籍描述

标签 python exception python-2.x python-2.5

我正在关注 Apress,从新手到专业的 Python 入门 这本书。据说:

finally. You can use try/finally if you need to make sure that some code (for example, cleanup code) is executed regardless of whether an exception is raised or not. This code is then put in the finally clause. Note that you cannot have both except clauses and a finally clause in the same try statement—but you can put one inside the other.

我试过这段代码:

def someFunction():
    a = None
    try:
        a = 1 / 0
    except ZeroDivisionError, e:
        print 'Yesss'
        print e
    finally:
        print 'Cleanup'
        del a

if __name__ == '__main__':
    someFunction()

...输出是

Yesss
integer division or modulo by zero
Cleanup

在这里,我在同一个try 段中使用了exceptfinally,不是吗?并且代码按预期工作正常。我不太明白书上说的是什么!

有人请澄清。谢谢。

最佳答案

自 python 2.5 以来已修复此问题,并在 documentation 中明确指出

换句话说,你的书不正确/过时了

关于python - Try-Except-Finally 语句的过时书籍描述,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7524728/

相关文章:

C++ 异常处理未按预期工作

silverlight - Silverlight 应用程序中出现间歇性 Async_ExceptionOccurred 错误的原因是什么?

python - int 和 tuple 之间的比较行为

python - 看起来相似的 Python for 循环之间的区别?

python - 分类任务,测试集分布不同时结果不同

python - 将外部函数分配给Python中的类变量

python - Selenium 无法在 Linux 上获取网页内容,但在 Windows 上可以很好地获取特定网站

java - 我必须编写自己的异常吗? - java

python - 如何使用 __subclasscheck__ 魔术方法?

python - 替换嵌套字典中的键