如何在Python中引发错误并通过自定义消息退出 [英] How to throw error and exit with a custom message in python
本文介绍了如何在Python中引发错误并通过自定义消息退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我见过有人在Python中建议sys.exit()。
我的问题是,还有其他方法可以退出当前脚本的执行,我的意思是终止,并出现错误。
I've seen people suggesting sys.exit() in Python. My question is that, is there any other way to exit the execution of current script, I mean termination, with an error.
类似这样的事情:
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()
推荐答案
使用字符串调用 sys.exit
将起作用。 文档明确提到了此用法:
Calling sys.exit
with a string will work. The docs mention this use explicitly:
尤其是sys.exit(某些错误消息)是在发生错误时退出程序的快速方法。
In particular, sys.exit("some error message") is a quick way to exit a program when an error occurs.
这篇关于如何在Python中引发错误并通过自定义消息退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文