overview strong> h3>
The
Python program has two exits: os._exit() and sys.exit(). I looked up the difference between the two approaches.
os._exit() terminates the python program directly, and none of the code after that executes.
sys.exit() throws an exception: SystemExit, and if the exception is not caught, the python interpreter exits. If there is code to catch the exception, it will still execute.
for example
import os
try:
os._exit(0)
except:
print('Program is dead.')
this print does not print because no exception is caught.
import sys
try:
sys.exit(0)
except:
print('Program is dead.')
finally:
print('clean-up')
Both print here because sys.exit() throws an exception.
Conclusion
strong> h3>
exits the program gracefully by using sys.exit(), which raises a SystemExit exception, which we can then catch and do some cleanup. Os._exit () simply exits the Python interpreter, and none of the following statements are executed.
, in general, use sys.exit(); Os._exit () can be used in the child process produced by os.fork().
reference:
p> [1] https://docs.python.org/3.5/library/exceptions.html
p> [2] http://www.cnblogs.com/gaott/archive/2013/04/12/3016355.html
import os
try:
os._exit(0)
except:
print('Program is dead.')
import sys
try:
sys.exit(0)
except:
print('Program is dead.')
finally:
print('clean-up')
exits the program gracefully by using sys.exit(), which raises a SystemExit exception, which we can then catch and do some cleanup. Os._exit () simply exits the Python interpreter, and none of the following statements are executed.
, in general, use sys.exit(); Os._exit () can be used in the child process produced by os.fork().
reference:
p> [1] https://docs.python.org/3.5/library/exceptions.html
p> [2] http://www.cnblogs.com/gaott/archive/2013/04/12/3016355.html