2014-03-25 72 views
12

我见过有人在Python中建议sys.exit()。 我的问题是,是否有任何其他方式退出当前脚本的执行,我的意思是终止,并带有错误。如何抛出错误并退出python

事情是这样的:

sys.exit("You can not have three process at the same time."); 

目前我的解决办法是:

print("You can not have three process at the same time."); 
sys.exit(); 
+3

反而引发异常?而对于这样的错误消息,我会写入'sys.stderr':'print('你不能同时拥有三个进程',file = sys.stderr)',并使用'sys.exit (1)'表示错误退出代码。 –

+0

http://docs.python.org/2/tutorial/errors.html或许 – tuxuday

+0

感谢您的帮助 –

回答

15

调用sys.exit用绳子将工作。该docs提及此用途明确:

特别是,sys.exit(“一些错误消息”)是一个快速的方法发生错误时退出程序。

+0

让我检查它 –

+0

感谢它的工作原理,因为我想。 –