关于控制台应用程序返回的错误代码的最佳实践是什么?

发布于 2024-08-08 07:23:53 字数 169 浏览 2 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淤浪 2024-08-15 07:23:53

零通常意味着没有错误。至于其他的,我认为没有保留集。我通常会定义应用程序的不同返回代码的含义并记录它们。

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.

苄①跕圉湢 2024-08-15 07:23:53

我见过最广泛使用返回码的是 Robocopy。它可以为其他应用程序提供指导。 Robocopy 使用返回码作为标志:

0×10 严重错误。 Robocopy 没有
复制任何文件。这要么是一种用法
错误或者由于不足而导致的错误
源的访问权限或
目标目录。

0×08 某些文件或目录可能
无法复制(发生复制错误
并且超出了重试限制)。
进一步检查这些错误。

0×04 一些不匹配的文件或
检测到目录。检查
输出日志。家政服务大概是
必要的。

0×02 一些额外的文件或目录
被检测到。检查输出日志。
可能需要一些内务处理。

0×01 复制了一个或多个文件
成功(即新文件已
到达)。

0×00 没有发生错误,也没有
复制完成。来源和
目标目录树是
完全同步。

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:

0×10 Serious error. Robocopy did not
copy any files. This is either a usage
error or an error due to insufficient
access privileges on the source or
destination directories.

0×08 Some files or directories could
not be copied (copy errors occurred
and the retry limit was exceeded).
Check these errors further.

0×04 Some Mismatched files or
directories were detected. Examine the
output log. Housekeeping is probably
necessary.

0×02 Some Extra files or directories
were detected. Examine the output log.
Some housekeeping may be needed.

0×01 One or more files were copied
successfully (that is, new files have
arrived).

0×00 No errors occurred, and no
copying was done. The source and
destination directory trees are
completely synchronized.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文