The argument to System.exit(), is return code to the invoking process. If you are program is being invoked by Operating system, then the return code will inform the OS, the status of execution.
The status of execution, may be, ( typically)
Successful - zero
Failure - -1
Failure + Information message, Positive numbers.
It is an indicator to inform the program exit status.
The API wrote:Terminates the currently running Java Virtual Machine. The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination.
You could certainly invent your own conventions about what various status codes mean, and perhaps somebody has invented the convention you describe there, but if they have I've never heard of it.
It's pretty uncommon for people to write Java applications where the System.exit() status code means something anyway, but I suppose if you were writing a lot of little Java applications which were meant to be run by shell scripts then a convention like that might be a useful thing.
you should avoid System.exit as much as possible; in a large or multi‑threaded application it can cause serious harm by terminating the JVM before all data have been saved, or similar.
Paul Clapham wrote:You could certainly invent your own conventions about what various status codes mean...
@Sultan: But you need to be very careful about that.
For starters: (as Campbell said) using System.exit() at all is not generally a good idea.
Secondly: Although the return code is nominally an int, some execution environments - Unix shell scripts being just one - may not recognise it as such. I'm pretty sure, for example, that bash only uses the last 8 bits (unless it's changed since I was using it), which means that the maximum code it can "recognise" is either 127 or 255.
So don't start designing elaborate return code systems until you've thoroughly tested them - on many platforms. :)
System.exit(any +ve number): Termination which you thought and might have gone wrong. Can be considered as an error.
System.exit(any -ve number): Termination which you've not at all expected to occur, might be due to h/w failure, others ,etc. Can be considered as an exception.
That is obviously the convention you are using in your current app. There will be different conventions for different people and it doesn't matter which you use as long as you retain 0 for normal exit.
Debdeep Ganguly wrote:System.exit(0): Termination under normal conditions.
System.exit(any +ve number): . . . .
System.exit(any -ve number): . . . .
But I still think frequent use of System.exit is a “code smell” and you are probably producing bad code. It is a bad idea to use System.exit at all.
Then, if Mary and I were working on a different project, she and I could come up with some OTHER set of codes that mean different things. But again, as long as Mary and I agree on what the codes are, everything is fine.
Now that we're in the more advanced programming world, these conventions and methodologies are a little outdated. We didn't HAVE exceptions back then. Inter-process communication was very complicated back then, so simply passing a number worked rather well.
Short answer though it: don't get hung up on the difference between the two. the number is simply a value being passed back to the calling program - which could be the OS, it could be another java program, it could be cron...