16

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.

gioele
  • 8,311
  • 4
  • 49
  • 75
MikeD
  • 883
  • 2
  • 12
  • 24

8 Answers8

17

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

Douglas Leeder
  • 49,001
  • 8
  • 86
  • 133
11

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.

Community
  • 1
  • 1
gioele
  • 8,311
  • 4
  • 49
  • 75
8

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.

Laurence Gonsalves
  • 125,464
  • 31
  • 220
  • 273
7

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.

David
  • 68,722
  • 16
  • 125
  • 165
  • 2
    @Lurkers: Despite this being a Windows question, I find it important to point out that while there is no hard standard (e.g. ISO or DIN), there do exist conventions for some operating systems. E.g., here are the standard exit codes for Linux: http://tldp.org/LDP/abs/html/exitcodes.html – Sebastian Mach Nov 29 '18 at 14:50
7

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.
Mark Amery
  • 110,735
  • 57
  • 354
  • 402
  • Or just cherry pick the few that are useful, and define those as your 'own' error codes that happen to re-use the System Error Codes. As long as your program has a predictable behaviour, no-one really cares about the implementation. – Pyro Aug 21 '18 at 14:22
4

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.

Will Eddins
  • 12,890
  • 5
  • 46
  • 83
1

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)

Pyro
  • 1,229
  • 1
  • 9
  • 7
  • 1
    You needn't use the *System Error Codes* as program exit codes. As noted in other answers here, it's conventional for Windows programs to use *1* as a catch-all error code; if everyone were using the *System Error Codes*, that would always mean "Incorrect function" (and there'd be no way at all to indicate a generic unspecified error). – Mark Amery May 09 '17 at 20:47
  • 1
    It's true, you don't need to comply with standards. The question was about standards, and that is what I provided. The base case is zero indicates success, non-zero indicates failure. If the caller cannot handle specific failures, you wouldn't care what the error was anyway, but if you expect callers to action the failure, it's good practice to give meaningful errors. Standards help give structure in such a case. – Pyro Jun 22 '17 at 15:57
  • 1
    *"you don't need to comply with standards"* - this is a non-sequitur; using System Error Codes as application exit codes does nothing to "comply with standards" because there is no standard, anywhere, that suggests doing so. *"if you expect callers to action the failure, it's good practice to give meaningful errors"* - I agree, but this is poorly achieved by using System Error Codes since there are thousands of them (so the caller can't possibly consider them all) and they are likely to be too generic to describe an application-specific error in a meaningful way. – Mark Amery Dec 16 '17 at 19:57
  • System Error Codes != Exit Codes – Henrik Holmgaard Høyer Nov 27 '19 at 10:36
  • 1
    Henrik is strictly correct, System Error Codes != Exit Codes. However, Microsoft also uses these values in _application_ code samples. https://docs.microsoft.com/en-us/dotnet/api/system.environment.exitcode I like to do this as well because then you can retrieve a meaningful string with `FormatMessage()`, or in C# `var ex = new Win32Exception(code); Console.WriteLine(ex.Message);`. Note that ERROR_SUCCESS is defined as 0, so this enumeration is compatible with batches that test the exit code directly using AND/OR logic. https://stackoverflow.com/a/17085933/420400 – Paul Williams May 29 '20 at 16:03
-2

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

Stephane Grenier
  • 14,426
  • 35
  • 98
  • 179