[英]Exit script from inside of try-catch block in Python 3
I'm trying to exit a script from inside a try: except:
block except it just goes on to the exception case. 我试图从
try: except:
块内部退出脚本,除了它会继续处理异常情况。
None of these... 都不是...
try:
exit()
except:
pass()
try:
quit()
except:
pass
import sys
try:
sys.exit()
except:
pass
...exit my script, they just go on to the except case. ...退出我的脚本,他们只是继续进行例外情况。
How would I exit my script from inside one of these blocks? 如何从这些块之一退出脚本?
All of these examples raise the SystemExit
exception and you are catching that exception, a blank except
clause will catch all exceptions. 所有这些示例都引发
SystemExit
异常,并且您正在捕获该异常,空白的except
子句将捕获所有异常。
This is the reason why you should always specify the exception you intend to catch or at least use except Exception
eg 这就是为什么您应该始终指定要捕获或至少使用的
except Exception
例如except Exception
try:
exit()
except Exception:
pass
try:
quit()
except Exception:
pass
import sys
try:
sys.exit()
except Exception:
pass
With that change in place, all of you examples will cause your Python app to exit 完成该更改后,所有示例都将导致您的Python应用退出
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.