I'd like to know the difference between the following in Java
System.exit(0);
System.exit(-1);
System.exit(1);
When do I have to use the above code appropriately?
I'd like to know the difference between the following in Java
System.exit(0);
System.exit(-1);
System.exit(1);
When do I have to use the above code appropriately?
System.exit(0) by convention, a nonzero status code indicates abnormal termination.
System.exit(1) -It means termination unsuccessful due to some error
A good gotcha is any error code > 255 will be converted to error code % 256. One should be specifically careful about this if they are using a custom error code > 255 and expecting the exact error code in the application logic. http://www.tldp.org/LDP/abs/html/exitcodes.html
Zero
=> Everything OkayPositive
=> Something I expected could potentially go wrong went wrong (bad command-line, can't find file, could not connect to server)Negative
=> Something I didn't expect at all went wrong (system error - unanticipated exception - externally forced termination e.g.kill -9
)(values greater than 128 are actually negative, if you regard them as 8-bit signed binary, or twos complement)
There's a load of good standard exit-codes here
System.exit(system call)
terminates the currently running Java virtual machine by initiating its shutdown sequence. The argument serves as a status code.By convention, a nonzero status code indicates abnormal termination.
Read More at Java
On Unix and Linux systems,
0
for successful executions and1
or higher for failed executions.A non-zero exit status code, usually indicates abnormal termination. if
n != 0
, its up to the programmer to apply a meaning to the various n's.From https://docs.oracle.com/javase/7/docs/api/java/lang/System.html.
Here is the answer.