This question already has an answer here:
Can any one share to me difference between System.exit(0)
and System.exit(-1)
it is helpful if you explain with example.
This question already has an answer here:
Can any one share to me difference between System.exit(0)
and System.exit(-1)
it is helpful if you explain with example.
It's just the difference in terms of the exit code of the process. So if anything was going to take action based on the exit code (where 0 is typically success, and non-zero usually indicates an error) you can control what they see.
As an example, take this tiny Java program, which uses the number of command line arguments as the exit code:
Now running it from a bash shell, where
&&
means "execute the second command if the first one is successful" we can have:If you run your Java program in Linux/Unix you can examine the result using
echo $?
command. This is why it is important to callSystem.exit(0)
(this is done for you if you don't) if everything is fine andSystem.exit(non-zero)
otherwise.The parameter of System.exit(int) is the return value of your program, which can be evaluated in batch jobs (usually for console programs). By convention, every value other than 0 inidaces that something went wrong.
System.exit(0) means it is a normal exit from a program.But System.exit(-1) means the exit may be due to some error. Any number other that zero means abnormal exit.