What “standard” application return/exit codes should an application support?

好久不见. 提交于 2019-12-23 07:09:03

问题


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?

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.


回答1:


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.




回答2:


I think the only standard is 0 for success and non-zero for failure. And that's more of a convention than a standard.




回答3:


Maybe you can adopt some of the Unix conventions.

In another answer, the user David suggested

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.

This is the list as it appears on a Debian system:

#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.




回答4:


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.




回答5:


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.

If your application logs errors appropriately, the exit code will likely be completely unnecessary to keep track of.




回答6:


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:

Typically, the caller sets the status value to 0 to indicate a normal exit, or to some other value to indicate an error.

Or the C# docs on Envrionment.Exit and Environment.ExitCode which variously state:

Use 0 (zero) to indicate that the process completed successfully.

and

The default value is 0 (zero), which indicates that the process completed successfully.

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 Codes in the Windows documentation.

Unlike some other answerers, I strongly advise against using the System Error Codes as application exit codes. Some notes about the System Error Codes:

  • Microsoft do 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 own applications or commands. While there are some examples of applications that do use 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 thousands of 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.



回答7:


There definitely are standard error codes defined for Windows.

A long time ago we used negative errors for specific 'custom' errors, but I doubt that that is good practice.

System Error Codes (Windows)




回答8:


Implement what you'll use. Anything else is superfluous.



来源:https://stackoverflow.com/questions/1538884/what-standard-application-return-exit-codes-should-an-application-support

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!