-2

Possible Duplicate:
Difference in System. exit(0) , System.exit(-1), System.exit(1 ) in java

Can anyboby please explain the use of system.exit(0)?
What will happen internaly when we call this method especially the argument value? 0,1,2,3.. etc

Community
  • 1
  • 1
Rajesh Gupta
  • 1
  • 1
  • 1
  • 2
  • 1
    In addition to the information provided by the answers, the following needs to be added: **do not call `System.exit`** at all unless you **specifically** need the Java process to report an error code. That use case is **exceedingly rare** and most probably you would be calling `System.exit` for no good reason. `System.exit` is a very raw, low-level, and unsafe way to end your program. It `stop`s all threads in the middle of whatever they were doing, giving them no chance to clean up. I have yet to write my first production-quality app that would make use of this call. – Marko Topolnik Oct 15 '12 at 10:10
  • This is not a duplicate of that question. – Ondra Žižka Feb 09 '18 at 15:57

3 Answers3

1

System.exit will ask the VM process to stop, and the code returned will be the code given in parameter. Common codes are: 0 for success, 1 to 127 for error, 128-255 is used by Unix and mapped to signals.

Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277
mkhelif
  • 1,551
  • 10
  • 18
0

System.exit(int) shuts down the JVM, providing an 'exit code' of 0.

The exit code is the JVM process's return value.

Usually in Unix systems, an exit code of 0 indicates a normal shutdown, and anything that is not zero indicates the shutdown was caused by an error.

See Wikipedia for more information:

http://en.wikipedia.org/wiki/Exit_status

HXCaine
  • 4,228
  • 3
  • 31
  • 36
0

The input to System.exit is your error code. A value of 0 means normal exit. A non zero number will indicate abnormal termination. This number can be up to you. Perhaps if you want to exit if you cannot read a file you could use error code =1, if you cannot read from a socket it could be error code = 2.

System.exit will terminate the VM and so your program.

A typical example could be below. If the runMyApp throws an exception where you want to cause the program to exit.

public static void main(String... args) {
   try {
      runMyApp();
   } catch (Exception e) {
      System.exit(1);
   }
}
RNJ
  • 15,272
  • 18
  • 86
  • 131