python sys.exit 在尝试中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25905923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:49:00  来源:igfitidea点击:

python sys.exit not working in try

pythonpython-2.7redhatexit

提问by onlyvinish

Python 2.7.5 (default, Feb 26 2014, 13:43:17)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> try:
...  sys.exit()
... except:
...  print "in except"
...
in except
>>> try:
...  sys.exit(0)
... except:
...  print "in except"
...
in except
>>> try:
...  sys.exit(1)
... except:
...  print "in except"
...
in except

Why am not able to trigger sys.exit() in try, any suggestions...!!!

为什么我不能在尝试中触发 sys.exit(),任何建议......!!!

The code posted here has all the version details.

此处发布的代码包含所有版本详细信息。

I have tried all possible ways i know to trigger it, but i failed. It gets to 'except' block.

我已经尝试了我知道的所有可能的方法来触发它,但我失败了。它进入“除外”块。

Thanks in advance..

提前致谢..

采纳答案by tamasgal

sys.exit()raises an exception, namely SystemExit. That's why you land in the except-block.

sys.exit()引发异常,即SystemExit. 这就是你降落在except-block的原因。

See this example:

看这个例子:

import sys

try:
    sys.exit()
except:
    print(sys.exc_info()[0])

This gives you:

这给你:

<type 'exceptions.SystemExit'>

Although I can't imagine that one has any practical reason to do so, you can use this construct:

尽管我无法想象有任何实际理由这样做,但您可以使用以下结构:

import sys

try:
    sys.exit() # this always raises SystemExit
except SystemExit:
    print("sys.exit() worked as expected")
except:
    print("Something went horribly wrong") # some other exception got raised

回答by Kasramvd

based on python wiki :

基于python wiki:

Since exit()ultimately “only” raises an exception, it will only exit the process when called from the main thread, and the exception is not intercepted.

由于exit()最终“只”抛出异常,所以只有从主线程调用时才会退出进程,不会拦截异常。

And:

和:

The exitfunction is not called when the program is killed by a signal, when a Python fatal internal error is detected, or when os._exit()is called.

exit当程序被信号杀死、检测到 Python 致命内部错误或被调用时,不会调用该函数os._exit()

Therefore, If you use sys.exit()within a tryblock python after raising the SystemExitexception python refuses of completing the exits's functionality and executes the exceptionblock.

因此,如果在引发异常后sys.exit()try块中使用SystemExitpython 将拒绝完成exits的功能并执行exception块。

Now, from a programming perspective you basically don't need to put something that you know definitely raises an exception in a tryblock. Instead you can either raise a SystemExitexception manually or as a more Pythonic approach if you don't want to loose the respective functionalities of sys.exit()like passing optional argument to its constructor you can call sys.exit()in a finally, elseor even exceptblock.

现在,从编程的角度来看,您基本上不需要将您知道肯定会引发异常的内容放入try块中。相反,SystemExit如果您不想失去sys.exit()将可选参数传递给其构造函数的相应功能,您可以手动引发异常或作为更 Pythonic 的方法,您可以sys.exit()在 a finallyelse甚至except块中调用。

Method 1 (not recommended)

方法一(不推荐)

try:
    # do stuff
except some_particular_exception:
    # handle this exception and then if you want 
    # do raise SystemExit
else:
    # do stuff and/or do raise SystemExit
finally:
    # do stuff and/or do raise SystemExit

Method 2 (Recommended):

方法二(推荐):

try:
    # do stuff
except some_particular_exception:
    # handle this exception and then if you want 
    # do sys.exit(stat_code)
else:
    # do stuff and/or do sys.exit(stat_code)
finally:
    # do stuff and/or do sys.exit(stat_code)