windows 应用程序应支持哪些“标准”应用程序返回/退出代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1538884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What "standard" application return/exit codes should an application support?
提问by MikeD
Is there such thing as a standard set of application return codes? Things like returning 0 for success 1 for failure, and then so on?
是否有一组标准的应用程序返回代码?诸如成功返回 0 失败返回 1 之类的事情?
I have a Windows Server application that I am adding some return error codes and wanted to stick to standard codes in addition to the app specific ones that I will need.
我有一个 Windows Server 应用程序,我正在添加一些返回错误代码,除了我需要的特定于应用程序的代码之外,我还想坚持使用标准代码。
采纳答案by David
There is no such thing as a standard set of exit codes that applications should conform to.
没有应用程序应该遵守的标准退出代码集。
However, there are some common ones like 0 for success as you mentioned. Depending on the Operating System and tools you use, you may be able to look at the exit codes for similar apps and mimic them.
但是,正如您提到的,有一些常见的成功,例如 0。根据您使用的操作系统和工具,您或许可以查看类似应用程序的退出代码并模仿它们。
回答by Douglas Leeder
I think the only standard is 0 for success and non-zero for failure. And that's more of a convention than a standard.
我认为唯一的标准是成功为 0,失败为非零。这更像是一种约定而不是标准。
回答by gioele
Maybe you can adopt some of the Unix conventions.
也许您可以采用一些 Unix 约定。
In another answer, the user David suggested
在另一个答案中,用户 David 建议
sysexits.h
has a list of standard exit codes. It seems to date back to at least 1993 and some big projects like Postfix use it, so I imagine it's the way to go.From the OpenBSD man page:
According to style(9), it is not good practice to call exit(3) with arbi- trary values to indicate a failure condition when ending a program. In- stead, the pre-defined exit codes from sysexits should be used, so the caller of the process can get a rough estimation about the failure class without looking up the source code.
sysexits.h
有一个标准退出代码列表。它似乎至少可以追溯到 1993 年,一些像 Postfix 这样的大项目使用它,所以我想这是要走的路。从 OpenBSD 手册页:
根据 style(9),在结束程序时使用任意值调用 exit(3) 来指示失败条件不是好的做法。相反,应该使用来自 sysexits 的预定义退出代码,这样进程的调用者可以在不查看源代码的情况下粗略估计故障类别。
This is the list as it appears on a Debian system:
这是在 Debian 系统上显示的列表:
#define EX_USAGE 64 /* command line usage error */
#define EX_DATAERR 65 /* data format error */
#define EX_NOINPUT 66 /* cannot open input */
#define EX_NOUSER 67 /* addressee unknown */
#define EX_NOHOST 68 /* host name unknown */
#define EX_UNAVAILABLE 69 /* service unavailable */
#define EX_SOFTWARE 70 /* internal software error */
#define EX_OSERR 71 /* system error (e.g., can't fork) */
#define EX_OSFILE 72 /* critical OS file missing */
#define EX_CANTCREAT 73 /* can't create (user) output file */
#define EX_IOERR 74 /* input/output error */
#define EX_TEMPFAIL 75 /* temp failure; user is invited to retry */
#define EX_PROTOCOL 76 /* remote error in protocol */
#define EX_NOPERM 77 /* permission denied */
#define EX_CONFIG 78 /* configuration error */
Inside the file /usr/include/sysexits.h
one can find more detailed descriptions of these error codes.
在文件中/usr/include/sysexits.h
可以找到这些错误代码的更详细描述。
回答by Laurence Gonsalves
The standard status code are EXIT_SUCCESS
and EXIT_FAILURE
, defined in stdlib.h
. Pretty much everyone just uses 0 and 1 respectively, though. Some software will use different non-zero code for different types of errors.
标准状态代码是EXIT_SUCCESS
和EXIT_FAILURE
,在 中定义stdlib.h
。不过,几乎每个人都分别使用 0 和 1。某些软件会针对不同类型的错误使用不同的非零代码。
回答by Will Eddins
Exit codes are far from standard, and are more used for the developer to know the appropriate error that has occurred upon return of the application. The standard of 0 for success, non-zero for failure is a general trend, and is used as it lets you use the full non-zero range for all possible errors.
退出代码远非标准,更多地用于开发人员了解应用程序返回时发生的适当错误。成功为 0,失败为非零的标准是普遍趋势,使用它是因为它允许您对所有可能的错误使用完整的非零范围。
If your application logs errors appropriately, the exit code will likely be completely unnecessary to keep track of.
如果您的应用程序适当地记录错误,退出代码可能完全不需要跟踪。
回答by Mark Amery
The only real convention is that 0
means success and non-zero values (typically 1
) mean failure. For an official reference on this, see, for instance, Microsoft's C++ docs on exit
:
唯一真正的约定是这0
意味着成功,非零值(通常1
)意味着失败。有关这方面的官方参考,例如,请参阅 Microsoft 的 C++ 文档exit
:
Typically, the caller sets the
status
value to 0 to indicate a normal exit, or to some other value to indicate an error.
通常,调用者将该
status
值设置为 0 以指示正常退出,或设置为其他值以指示错误。
Or the C# docs on Envrionment.Exit
and Environment.ExitCode
which variously state:
或者 C# docs on Envrionment.Exit
and Environment.ExitCode
which different states:
Use 0 (zero) to indicate that the process completed successfully.
使用 0(零)表示该过程成功完成。
and
和
The default value is 0 (zero), which indicates that the process completed successfully.
默认值为 0(零),表示该过程成功完成。
and
和
Use a non-zero number to indicate an error. In your application, you can define your own error codes in an enumeration, and return the appropriate error code based on the scenario. For example, return a value of 1 to indicate that the required file is not present and a value of 2 to indicate that the file is in the wrong format. For a list of exit codes used by the Windows operating system, see System Error Codesin the Windows documentation.
使用非零数字表示错误。在您的应用程序中,您可以在枚举中定义自己的错误代码,并根据场景返回相应的错误代码。例如,返回值 1 表示所需文件不存在,返回值 2 表示文件格式错误。有关 Windows 操作系统使用的退出代码列表,请参阅Windows 文档中的系统错误代码。
Unlike some other answerers, I strongly advise against using the System Error Codesas application exit codes. Some notes about the System Error Codes:
与其他一些回答者不同,我强烈建议不要使用系统错误代码作为应用程序退出代码。关于系统错误代码的一些注意事项:
- Microsoftdo not advise using them as application exit codes anywhere, and indeed explicitly suggest that you "define your own error codes"in the documentation I quote above.
- Microsoft don't use them consistently as exit codes in their ownapplications or commands. While there are some examples of applications that douse these codes, like MsiExec.exe, there are plenty more that don't, like
dir
,dotnet
, or TAEF. - Using them seems like a manifestly bad idea to me. There are thousandsof System Exit Codes, most of which are irrelevant to whatever your particular application is. If you try to use them, you're going to waste ages picking through the list to find codes that apply to your scenario, and the end result will be less useful to a developer calling your application than if you had just defined a small number of exit codes that are meaningful for your particular application - so do that instead.
- Microsoft不建议在任何地方将它们用作应用程序退出代码,并且确实明确建议您在我上面引用的文档中“定义自己的错误代码”。
- Microsoft 不会在他们自己的应用程序或命令中始终将它们用作退出代码。虽然有一些应用的一些例子做使用这些代码,比如msiexec.exe的,有很多更不一样
dir
,dotnet
或塔伊夫。 - 使用它们对我来说似乎是一个明显的坏主意。有数以千计的系统退出代码,其中大部分与您的特定应用程序无关。如果您尝试使用它们,您将浪费时间在列表中挑选适用于您的场景的代码,并且最终结果对于调用您的应用程序的开发人员来说比您刚刚定义一个小数字更没用对您的特定应用程序有意义的退出代码 - 而是这样做。
回答by Pyro
There definitely are standard error codes defined for Windows.
肯定有为 Windows 定义的标准错误代码。
A long time ago we used negative errors for specific 'custom' errors, but I doubt that that is good practice.
很久以前,我们对特定的“自定义”错误使用了否定错误,但我怀疑这是一种很好的做法。
回答by Stephane Grenier
Implement what you'll use. Anything else is superfluous.
实施您将使用的内容。其他的都是多余的。