问题
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
回答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.
回答2:
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
回答3:
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);
}
}
来源:https://stackoverflow.com/questions/12893350/when-to-use-system-exit0