Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference in System. exit(0) , System.exit(-1), System.exit(1 ) in Java

People also ask

What is the difference between Exit 0 and exit (- 1?

Both the functions, exit(0) and exit(1), are used to exit out of the program, but there is one major difference between exit(0) and exit(1). The exit (0) shows the successful termination of the program and the exit(1) shows the abnormal termination of the program.

What does system Exit 0 do in Java?

As mentioned earlier, System. exit(0) method terminates JVM which results in termination of the currently running program too. Status is the single parameter that the method takes. If the status is 0, it indicates the termination is successful.

What is the meaning of system Exit 1 in Java?

exit(0) : Generally used to indicate successful termination. exit(1) or exit(-1) or any other non-zero value – Generally indicates unsuccessful termination. Note : This method does not return any value. The following example shows the usage of java. lang.

What is the difference between system Exit 0 and break?

break causes an immediate exit from the switch or loop (for, while or do). exit() terminates program execution when it is called.


The parameter of exit should qualify if the execution of the program went good or bad. It's a sort of heredity from older programming languages where it's useful to know if something went wrong and what went wrong.

Exit code is

  • 0 when execution went fine;
  • 1, -1, whatever != 0 when some error occurred, you can use different values for different kind of errors.

If I'm correct exit codes used to be just positive numbers (I mean in UNIX) and according to range:

  • 1-127 are user defined codes (so generated by calling exit(n))
  • 128-255 are codes generated by termination due to different unix signals like SIGSEGV or SIGTERM

But I don't think you should care while coding on Java, it's just a bit of information. It's useful if you plan to make your programs interact with standard tools.


Zero => Everything Okay

Positive => 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.

  System.exit(0) or EXIT_SUCCESS;  ---> Success
  System.exit(1) or EXIT_FAILURE;  ---> Exception
  System.exit(-1) or EXIT_ERROR;   ---> Error

Read More at Java

On Unix and Linux systems, 0 for successful executions and 1 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.


exit(0) generally used to indicate successful termination. exit(1) or exit(-1) or any other non-zero value indicates unsuccessful termination in general.