ITPub博客

首页 > IT职业 > IT生活 > final作用域的代码一定会被执行吗?

final作用域的代码一定会被执行吗?

IT生活 作者:cylong 时间:2021-09-11 10:37:58 0 删除 编辑

正常的情况下,finally作用域的代码一定会被执行的,不管是否发生异常。哪怕是调用了sys.exit函数,finally也是会被执行的。

那怎么样才能让finally代码不执行了。

import time
choice = True
try:
    if choice:
        while True:
            pass
    else:
        print "Please pull the plug on your computer sometime soon..."
        time.sleep(60 * 60 * 24 * 365)
finally:
    print "Finally ..."

上面的代码主要是通过让流程停滞在try作用域里,从而实现了需求。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2005/viewspace-2837488/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-16

  • 博文量
    106
  • 访问量
    1358