This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
there is no documentation about the values you can send. it's up to you. if you want to you can use System.exit(12345) to terminate your program in OK cases and System.exit(0) in NOT OK cases.... it's up to you.
The parameter becomes the "exit status" of the process -- it's sent back to the operating system as-is, and Java doesn't even look at it.
By convention, 0 means "normal termination" and anything else means "error", but not all programs conform to that. It's 100% up to the individual program to choose different exit values and assign meaning to them. The returned value is most often simply ignored.
In practice, you'll rarely see anything but System.exit(0); when you do, it's generally exit(-1) to indicate an error.
The parameter you use there will (usually) be passed back to whatever called your Java program under the name of something like "error code". And that "whatever", which is often a script or shell program, can do something based on the value.
Traditionally zero means "Terminated normally". Non-zero values are up to you to specify.
I see a few people "snuck in" with answers while I was writing mine. I'll add some shell/bat scripting to show how it might work. ----
Well it all depends on how the return value of the executable is to be used. On Unix systems, for instance, a return value of 0 means that the program was successful. In sh, bash, etc., an exit value of 0 is "true" (contrary to the C language standard).
So something like...
would work as one might expect if LookForFile did an System.exit(0) if the file referenced by the command line argument exists and System.exit(1) if the file does NOT exist.
In Windows BAT files, the errorlevel command is used to check the exit value of the previous program and returns "true" if the exit value was greater than or equal to the numeric argument. The shell script above would translate to something like this in a Windows bat file.
Again the standard is to exit with a zero if everything was successful. The menaing of any other exit value depends on what the program does. [ August 02, 2006: Message edited by: Ryan McGuire ]
No, that's not it at all -- you're making it vastly more complex than it really is.
As I said, the JVM does not interpret the argument to System.exit(). It doesn't matter what value you pass: the program will immediately terminate and return that value to the operating system. That's it.
The OS, or a script or program that launched your Java program, might then look at that value and decide to do something about it -- but at that time, your Java program is already gone.
Joined: Jul 10, 2006
Ok, so then let's say that we want to throw an exception to the main(). The code for System.exit would be there. If an exception is thrown, then we would code 'System.exit(-1)'. If there is no exception thrown, we would code 'System.exit(0)'. So, then, I should really learn how to throw an exception so that System.exit(?) gets set to what ever value we determine. Is this correct?
author and iconoclast
Note that if a program exits due to an uncaught exception in main(), Sun's JVMs do, actually, return "1" as an exit status; if it terminates normally, then "0" is returned. So in fact, the above isn't really necessary.