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.