java 什么时候使用 system.exit(0)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12893350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When to use system.exit(0)?
提问by Rajesh Gupta
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
谁能解释一下 system.exit(0) 的用法?
当我们调用这个方法特别是参数值时,内部会发生什么?0,1,2,3...等
回答by mkhelif
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.
System.exit 会要求 VM 进程停止,返回的代码将是参数中给出的代码。常见的代码有:0 表示成功,1 到 127 表示错误,128-255 被 Unix 使用并映射到信号。
回答by RNJ
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
是您的错误代码。值为 0 表示正常退出。非零数字表示异常终止。这个数字可以由你决定。也许如果您想在无法读取文件的情况下退出,则可以使用错误代码 = 1,如果无法从套接字读取,则可能是错误代码 = 2。
System.exit
will terminate the VM and so your program.
System.exit
将终止 VM,因此您的程序。
A typical example could be below. If the runMyApp throws an exception where you want to cause the program to exit.
下面是一个典型的例子。如果 runMyApp 在您想要导致程序退出的地方抛出异常。
public static void main(String... args) {
try {
runMyApp();
} catch (Exception e) {
System.exit(1);
}
}
回答by HXCaine
System.exit(int) shuts down the JVM, providing an 'exit code' of 0.
System.exit(int) 关闭 JVM,提供 0 的“退出代码”。
The exit code is the JVM process's return value.
退出代码是 JVM 进程的返回值。
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.
通常在 Unix 系统中,退出代码 0 表示正常关闭,任何非零表示关闭是由错误引起的。
See Wikipedia for more information:
有关更多信息,请参阅维基百科: