ExitCodes bigger than 255, possible?

If yes, on which operating system, shell or whatever?

Consider the following java program (I'm using java just as an example, any language would be good for this question, which is more about operation systems):

public class ExitCode {
    public static void main(String args[]) {
        System.exit(Integer.parseInt(args[0]));
    }
}

Running it on Linux and bash, it returns always values less equal 255, eg ( echo $? prints the exit code of the previous executed command)

> java ExitCode 2; echo $?
2

> java ExitCode 128; echo $?
128

> java ExitCode 255; echo $?
255

> java ExitCode 256; echo $?
0

> java ExitCode 65536; echo $?
0

EDITED: the (only, so far) answer below fully explain what happens on UNIXes. I'm still wondering about other OSes.


Not possible on Unix and derivatives. The exit status information returned consists of two 8-bit fields, one containing the exit status, and the other containing information about the cause of death (0 implying orderly exit under program control, other values indicating that a signal killed it, and indicating whether a core was dumped).


On modern Windows, the OS itself, and the default console shell ( CMD.EXE ), accept and show exit codes throughout at least the whole range of 32-bit signed integers. Running your example above in CMD.EXE gives the exit codes you asked for :

> java ExitCode 2
> echo %errorlevel%
2

> java ExitCode 128
> echo %errorlevel%
128

> java ExitCode 255
> echo %errorlevel%
255

> java ExitCode 256
> echo %errorlevel%
256

> java ExitCode 65536
> echo %errorlevel%
65536

Windows doesn't really have the concept of Unix signals, nor does it try to hijack the exit code to add extra information, so as long as your shell (or whatever program ends up reading the exit code) doesn't do that either, you should get back the exit codes you returned. Fortunately, programs that use Microsoft's C runtime (including all programs compiled with MS Visual C++) preserve the exit code as is from exiting processes.


Windows has many more exit codes, over 14,000. (I'm sure you often saw some of them on your own screen).

Here comes:

  • A list of Windows exit codes.
  • PowerShell script to test them.
  • 链接地址: http://www.djcxy.com/p/28740.html

    上一篇: 在ISO C ++中从main返回0的原因是什么?

    下一篇: ExitCodes大于255,可能吗?