2011-09-29 105 views
2
def main(): 
    try: 
     print "hardfart" 
     return 0 
    except: 
     return 1 

if __name__ == '__main__': 
    exit(main()) 

某種程序員可以告訴我爲什麼會在退出時吐出以下錯誤消息嗎?退出時出現SystemExit和NameError問題

Traceback (most recent call last): 
File "C:/Apps/exp_exit.py", line 9, in ,module. 
exit(main()) 
File "C:\Apps\python2.7.2\lib\site.py", line 372 in __call__ 
    raise SystemExit(code) 
SystemExit: 0 

這在設置類似的項目中導致退出時出錯。對於該項目,使用gui2exe編譯一個exe後,關閉程序時,我得到這個相關的錯誤:

Traceback (most recent call last): 
    File "checkHDBox.py", line 303, in <module> 
NameError: name 'exit' is not defined 

因此,如果出口處產生這個錯誤,我怎麼退出呢?如果我爲退出創建一個異常處理程序,是否不會取代python用退出函數執行的默認操作?

謝謝。

編輯:

我認爲這回答了我自己的問題。

這裏的追蹤是從空閒,我認爲這是從我讀過的其他來源的默認行爲。

Traceback (most recent call last): 
File "C:/Apps/exp_exit.py", line 9, in ,module. 
exit(main()) 
File "C:\Apps\python2.7.2\lib\site.py", line 372 in __call__ 
    raise SystemExit(code) 
SystemExit: 0 

回溯這裏固定使用sys.exit(),而不是出口(0)

Traceback (most recent call last): 
    File "checkHDBox.py", line 303, in <module> 
NameError: name 'exit' is not defined 

回答

1

您退出通過提高SystemExit的程序。這就是exit()所做的。有人錯誤地編寫了捕獲所有異常的異常處理程序。 這就是爲什麼你只捕捉你可以處理的例外。

+1

劃痕。這個追蹤來自IDLE,你知道這是否意味着它的功能,以便在任何退出調用時追蹤回溯? –