正常的情况下,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作用域里,从而实现了需求。
Copyright © 2019- fupindai.com 版权所有 赣ICP备2024042792号-2
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务