views:

1731

answers:

4

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, e.g. (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.

+10  A: 

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

Jonathan Leffler
+2  A: 

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

Here comes:

Veynom
You are talking about the error codes returned by the standard Windows APIs. The OP wants to know what error codes (s)he can return for his/her own program.
crosstalk
+5  A: 

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.

crosstalk
+1  A: 

http://en.wikipedia.org/wiki/Exit_status http://www.faqs.org/docs/abs/HTML/exitcodes.html#EXITCODESREF

lyman