Difference between System.exit(-1), System.exit(0) and System.exit(1) in java



Contents of page :


A) Difference between System.exit(-1), System.exit(0) and System.exit(1) methods in java.


System.exit(-1)>
  • If exit method returns -1 or some other negative number that means some error occurred,
  • The error occurred which user could NOT have expected.
  • Its Abnormal termination.


System.exit(0) >
  • If exit method returns 0 that means method execution was successful.
  • Its Normal termination.


System.exit(1) >
  • If exit method returns 1 or some other positive number that means some exception occurred,
  • The exception occurred which user could have expected.
  • These may be user defined codes to indicate the exception occurred which user could have expected.
  • Its Abnormal termination.

B) Let’s learn about System.exit(n) method in java >
  • System.exit terminates JVM.
  • Parameter
    • Passing zero as parameter means normal termination &
    • Passing non-zero as parameter means abnormal termination.
  • System.exit(n) internally calls Runtime.getRuntime().exit(n)

Also read Program to show what will happen when catch and finally both return some value. Click here.

SUMMARY >
Difference between System.exit(-1), System.exit(0) and System.exit(1) methods in java.
System.exit(-1)>
    • Abnormal termination. The error occurred which user could NOT have expected.


System.exit(0) >
  • Normal termination. Method execution was successful.


System.exit(1) >
  • Abnormal termination. The exception occurred which user could have expected.


RELATED LINKS>



eEdit
Must read for you :