Python os._exit, sys.exit

在Python2.7脚本最后 运行语句sys.exit(0)或者运行exit()或者exit(0)的时候会出现结果:SystemExit

exception SystemExit

This exception is raised by the sys.exit() function. When it is not handled, the Python interpreter exits; no stack traceback is printed. If the associated value is a plain integer, it specifies the system exit status (passed to C’s exit() function); if it is None, the exit status is zero; if it has another type (such as a string), the object’s value is printed and the exit status is one.

Instances have an attribute code which is set to the proposed exit status or error message (defaulting to None). Also, this exception derives directly from BaseException and not StandardError, since it is not technically an error.

A call to sys.exit() is translated into an exception so that clean-up handlers (finally clauses of try statements) can be executed, and so that a debugger can execute a script without running the risk of losing control. The os._exit() function can be used if it is absolutely positively necessary to exit immediately (for example, in the child process after a call to fork()).

The exception inherits from BaseException instead of StandardError or Exception so that it is not accidentally caught by code that catches Exception. This allows the exception to properly propagate up and cause the interpreter to exit.

Changed in version 2.5: Changed to inherit from BaseException.

os._exit() 程序会直接结束 而使用sys.exit(),exit(),exit() 会类似于抛出个异常
Python2.5之前的程序:
>>> import sys
>>> sys.exit()

下面用代码测试一下结果

====================

if __name__ == "__main__":
       try:
               print "try"
               sys.exit(0)
       except Exception, e:
               print "except"
       finally:
               print "finally"

output:

try
finally


---------------------

import sys
import os

if __name__ == "__main__":
       try:
               print "try"
               os._exit(0)
       except Exception, e:
               print "except"
       finally:
               print "finally"

output:

try

-----------------------

函数英文解释

os._exit( n)
Exit to the system with status n, without calling cleanup handlers, flushing stdio buffers, etc. Availability: Macintosh, Unix, Windows.
 
Note: The standard way to exit is sys.exit(n). _exit() should normally only be used in the child process after a fork().
sys.exit( [arg])
Exit from Python. This is implemented by raising the SystemExit exception, so cleanup actions specified by finally clauses of try statements are honored, and it is possible to intercept the exit attempt at an outer level.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值