当python finally遇到break和sys.exit

来源:互联网 发布:mac外置光驱读不了光盘 编辑:程序博客网 时间:2024/05/17 08:50
except lite.Error, e:    print "Error %s:" % e.args[0]    sys.exit(1)    print '++++++++++++++++++++++'finally:    print "---------------"


finally会不会执行?

在google搜:sys.exit finally第一个:

http://stackoverflow.com/questions/7709411/why-finally-block-is-executing-after-calling-sys-exit0-in-except-block

All sys.exit() does is raise an exception of type SystemExit.!

python很聪明的!

另外一种情况在循环中break

import Queueimport threadingimport timeimport sysworkQueue = Queue.Queue(10)for i in range(0,6):    workQueue.put(i)    def thread_get():    print 'i am starting------'    while  True:        try:            req = workQueue.get()            break            print 'aaaaaaaaa'        except Exception,qe:            print 'enmpty-----------'            break        finally:            print 'i finally'            workQueue.task_done()            for i in range(0,5):    t = threading.Thread(target=thread_get)    t.setDaemon(True)    t.start()workQueue.join()print "Exiting Main Thread"
        
       Py官网说的:When a returnbreak or continue statement is executed in the try suite of a try...finally statement, the finally clause is also executed ‘on the way out.’ A continue statement is illegal in the finally clause. (The reason is a problem with the current implementation — this restriction may be lifted in the future).

经过测试break后退出,造成task_done 死等

第一次测试竟然通过,,细查只正好放了5个数据进Queue!改成6个死锁!



原创粉丝点击