确保执行(finally)

异常处理

def throw_if_true(param):
    try:
        if param:
            raise OSError("test exception")
    except OSError:
        print("except")
    finally:
        print("finally")


throw_if_true(True)
# 输出 "except" 和 "finally"
throw_if_true(False)
# 只输出 "finally"