os._exit(), sys.exit() 是python中的两种 程序 退出方式
sys.exit()会引发一个异常:SystemExit,如果这个异常没有被捕获的话,python解释器将会退出。假如捕获此异常,后边代码还是会执行 status 0为正常退出,其他数值(1-127)为不正常,可抛异常事件供捕获。
import os, sys
try:
print(colour)
except:
sys.exit(0)
finally:
print('>>>>这一句会执行,后退出')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
>>>>这一句会执行,后退出
os._exit()会直接将python程序终止,之后的所有代码都不会执行,下边的代码 将没有打印输出
import os, sys
try:
print(colour)
except:
os._exit(0)
finally:
print('>>>>程序在 os._exit(0) 上直接退出,运行将不会有此行打印输出')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')