关于控制台应用程序返回的错误代码的最佳实践是什么?
在 C# 中,您可以使用 Environment.Exit(n) 返回一个整数给底层调用者(例如,可以使用 DOS 脚本中的 ERRORLEVEL 变量进行测试)。
是否有关于这些代码的最佳实践?
我认为 0 = 没有错误...但是有保留的数字吗?我可以使用负数吗?等
谢谢!
In c# you can return an integer to the underlying caller using Environment.Exit(n) (which can be tested, for instance, using the ERRORLEVEL variable from a DOS script).
Are there best practices regarding those codes?
I think 0 = no error... but are there numbers reserved? Can I use negative numbers? etc
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
零通常意味着没有错误。至于其他的,我认为没有保留集。我通常会定义应用程序的不同返回代码的含义并记录它们。
DOS 最多只能处理 256 个可能的返回码,但实际上我怀疑您是否会使用/需要这么多。
您可以使用负值,但请注意负值并不一定意味着与正值不同。
Zero usually means that there were no errors. As for others, I don't think there is a reserved set. I would usually define what the different return codes of my application meant and document them.
DOS only handles a maximum of 256 possible return codes, but in reality I doubt you would ever use/need that many.
You can use negative values, but note that negative values do not necessarily mean anything different from positive values.
我见过最广泛使用返回码的是 Robocopy。它可以为其他应用程序提供指导。 Robocopy 使用返回码作为标志:
The most extensive use of return codes I've seen is in Robocopy. It may provide guidance for other apps. Robocopy uses return codes as flags: