python - 如何在python中抛出错误并使用自定义消息退出

标签 python error-handling

我看到有人建议在 Python 中使用 sys.exit()。 我的问题是,有没有其他方法可以退出当前脚本的执行,我的意思是终止,出现错误。

类似这样的:

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 将起作用。 docs明确提及此用途:

In particular, sys.exit("some error message") is a quick way to exit a program when an error occurs.

关于python - 如何在python中抛出错误并使用自定义消息退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22633544/

相关文章:

error-handling - 用于获取工作日志的 JIRA REST API - "You do not have the permission to see the specified issue"

javascript - 使用 Gulp Notify 和 Plumber 的全局错误消息

python - 类中的文档字符串或 __init__ 构造函数?

python - 如何使用 groupby 计算平均每周支出,周为周一至周日?

python - 如何在 Visual Studio pyproj 项目中设置自定义构建步骤

python - 如何将自定义类型添加到 dill 的 pickleable 类型

python - Spyder 变量资源管理器如何显示自定义数据类型?

JavaScript 尝试/捕获 : errors or exceptions?

C++ 字符串流错误处理

c++ - 函数返回 std::wstring = NULL;