1》sys.exit()
>>> import sys
>>> help(sys.exit)
Help on built-in function exit in module sys:
exit(...)
exit([status])
Exit the interpreter by raising SystemExit(status).
If the status is omitted or None, it defaults to zero (i.e., success).
If the status is an integer, it will be used as the system exit status.
If it is another kind of object, it will be printed and the system
exit status will be one (i.e., failure).
執行該語句會直接退出程序,這也是經常使用的方法,也不需要考慮平台等因素的影響,一般是退出Python程序的首選方法。
退出程序引發SystemExit異常,(這是唯一一個不會被認為是錯誤的異常), 如果沒有捕獲這個異常將會直接退出程序執行,
當然也可以捕獲這個異常進行一些其他操作(比如清理工作)。
sys.exit()函數是通過拋出異常的方式來終止進程的,也就是說如果它拋出來的異常被捕捉到了的話程序就不會退出了,
而是去進行一些清理工作。
SystemExit 並不派生自Exception 所以用Exception捕捉不到該SystemEixt異常,應該使用SystemExit來捕捉。
該方法中包含一個參數status,默認為0,表示正常退出, 其他都是異常退出。
還可以這樣使用:sys.exit("Goodbye!"); 一般主程序中使用此退出.
捕獲到SystemExit異常,程序沒有直接退出!
song@ubuntu:~$ vi systemExit.py
song@ubuntu:~$ more systemExit.py
#!/usr/bin/python
#!coding:utf-8
import sys
if __name__=='__main__':
try:
sys.exit(825)
except SystemExit,error:
print 'the information of SystemExit:{0}'.format(error)
print "the program doesn't exit!"
print 'Now,the game is over!'
song@ubuntu:~$ python systemExit.py
the information of SystemExit:825
the program doesn't exit!
Now,the game is over!
song@ubuntu:~$
沒有捕獲到SystemExit異常,程序直接退出,后邊的代碼不執行!
song@ubuntu:~$ vi systemExit.py
song@ubuntu:~$ more systemExit.py
#!/usr/bin/python
#!coding:utf-8
import sys
if __name__=='__main__':
try:
sys.exit(825)
except Exception,error:
print 'the information of SystemExit:{0}'.format(error)
print "the program doesn't exit!"
print 'Now,the game is over!'
song@ubuntu:~$ python systemExit.py
song@ubuntu:~$
沒有捕獲到SystemExit異常,輸出'Goodbye!'后,程序直接退出,后邊的代碼不執行!
song@ubuntu:~$ vi systemExit.py
song@ubuntu:~$ more systemExit.py
#!/usr/bin/python
#!coding:utf-8
import sys
if __name__=='__main__':
try:
sys.exit('Goodbye!')
except Exception,error:
print 'the information of SystemExit:{0}'.format(error)
print "the program doesn't exit!"
print 'Now,the game is over!'
song@ubuntu:~$ python systemExit.py
Goodbye!
song@ubuntu:~$
2》os._exit(), 直接退出 Python 解釋器, 不拋異常, 不執行相關清理工作,其后的代碼都不執行,
其使用會受到平台的限制,但我們常用的Win32平台和基於UNIX的平台不會有所影響, 常用在子進程的退出.
一般來說os._exit() 用於在線程中退出,sys.exit() 用於在主線程中退出。
>>> import sys
>>> help(sys.exit)
Help on built-in function exit in module sys:
exit(...)
exit([status])
Exit the interpreter by raising SystemExit(status).
If the status is omitted or None, it defaults to zero (i.e., success).
If the status is an integer, it will be used as the system exit status.
If it is another kind of object, it will be printed and the system
exit status will be one (i.e., failure).
執行該語句會直接退出程序,這也是經常使用的方法,也不需要考慮平台等因素的影響,一般是退出Python程序的首選方法。
退出程序引發SystemExit異常,(這是唯一一個不會被認為是錯誤的異常), 如果沒有捕獲這個異常將會直接退出程序執行,
當然也可以捕獲這個異常進行一些其他操作(比如清理工作)。
sys.exit()函數是通過拋出異常的方式來終止進程的,也就是說如果它拋出來的異常被捕捉到了的話程序就不會退出了,
而是去進行一些清理工作。
SystemExit 並不派生自Exception 所以用Exception捕捉不到該SystemEixt異常,應該使用SystemExit來捕捉。
該方法中包含一個參數status,默認為0,表示正常退出, 其他都是異常退出。
還可以這樣使用:sys.exit("Goodbye!"); 一般主程序中使用此退出.
捕獲到SystemExit異常,程序沒有直接退出!
song@ubuntu:~$ vi systemExit.py
song@ubuntu:~$ more systemExit.py
#!/usr/bin/python
#!coding:utf-8
import sys
if __name__=='__main__':
try:
sys.exit(825)
except SystemExit,error:
print 'the information of SystemExit:{0}'.format(error)
print "the program doesn't exit!"
print 'Now,the game is over!'
song@ubuntu:~$ python systemExit.py
the information of SystemExit:825
the program doesn't exit!
Now,the game is over!
song@ubuntu:~$
沒有捕獲到SystemExit異常,程序直接退出,后邊的代碼不執行!
song@ubuntu:~$ vi systemExit.py
song@ubuntu:~$ more systemExit.py
#!/usr/bin/python
#!coding:utf-8
import sys
if __name__=='__main__':
try:
sys.exit(825)
except Exception,error:
print 'the information of SystemExit:{0}'.format(error)
print "the program doesn't exit!"
print 'Now,the game is over!'
song@ubuntu:~$ python systemExit.py
song@ubuntu:~$
沒有捕獲到SystemExit異常,輸出'Goodbye!'后,程序直接退出,后邊的代碼不執行!
song@ubuntu:~$ vi systemExit.py
song@ubuntu:~$ more systemExit.py
#!/usr/bin/python
#!coding:utf-8
import sys
if __name__=='__main__':
try:
sys.exit('Goodbye!')
except Exception,error:
print 'the information of SystemExit:{0}'.format(error)
print "the program doesn't exit!"
print 'Now,the game is over!'
song@ubuntu:~$ python systemExit.py
Goodbye!
song@ubuntu:~$
2》os._exit(), 直接退出 Python 解釋器, 不拋異常, 不執行相關清理工作,其后的代碼都不執行,
其使用會受到平台的限制,但我們常用的Win32平台和基於UNIX的平台不會有所影響, 常用在子進程的退出.
一般來說os._exit() 用於在線程中退出,sys.exit() 用於在主線程中退出。
3》exit()/quit(), 拋出SystemExit異常. 一般在交互式shell中退出時使用.
友情鏈接:
關於python中format()方法的使用,參考:python format()方法
(完)