python try exception finally记录
try exception finally中,finally下的语句块始终会执行
测试finally代码
def test_try_exception(a, b): '''测试异常捕获语句''' result = -1 try: result = a/b # 流程语句 except ZeroDivisionError as e: return 0 # 异常时执行 else: return result # 无异常时执行 finally: return 1 # 无论如何都会执行,上面的else中返回语句会执行,但执行完后还会执行finally中语句 if __name__ == '__main__': print(test_try_exception(1,2))
python3文档说明
参考
Become a Linux Programmer