异常:try vs finally,程序控制权的争夺

计算机 python 异常 编程



	经典代码(同样适用于Java、JavaScript):

def lol():
    try:
        return True
    finally:
        return False

	调用lol()后,究竟是返回False还是True呢?相信你和我一样,最开始认为是True,因为try中的语句先执行,对吧?

	但事实上:

>>> lol()
	False

try had a try. Finally, finally won.

	所以,不要在异常语句中用return……

__RAW_HTML__

Page created on 2011-12-26