系统差异。Java 中的 exit(0) , System.exit(-1), System.exit(1)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2434592/
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
Difference in System. exit(0) , System.exit(-1), System.exit(1 ) in Java
提问by giri
I'd like to know the difference between the following in Java
我想知道以下 Java 中的区别
System.exit(0);
System.exit(-1);
System.exit(1);
When do I have to use the above code appropriately?
我什么时候必须适当地使用上面的代码?
回答by Hyman
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.
0
执行顺利时;1
,-1
,whatever != 0
当发生某些错误时,您可以为不同类型的错误使用不同的值。
If I'm correct exit codes used to be just positive numbers (I mean in UNIX) and according to range:
如果我是正确的,退出代码曾经只是正数(我的意思是在 UNIX 中)并根据范围:
1-127
are user defined codes (so generated by callingexit(n)
)128-255
are codes generated by termination due to different unix signals like SIGSEGVor SIGTERM
1-127
是用户定义的代码(因此通过调用生成exit(n)
)128-255
是由于SIGSEGV或SIGTERM等不同的 unix 信号而由终止产生的代码
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.
但是我认为您在使用 Java 编码时不应该关心,这只是一些信息。如果您打算让您的程序与标准工具交互,它会很有用。
回答by Frederik Wordenskjold
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.
非零退出状态代码,通常表示异常终止。if n != 0
,由程序员决定对各种 n 应用含义。
From https://docs.oracle.com/javase/7/docs/api/java/lang/System.html.
来自https://docs.oracle.com/javase/7/docs/api/java/lang/System.html。
回答by robert
Zero
=> Everything Okay
Zero
=> 一切正常
Positive
=> Something I expected could potentially go wrong went wrong (bad command-line, can't find file, could not connect to server)
Positive
=> 我预期可能会出错的事情出错了(错误的命令行,找不到文件,无法连接到服务器)
Negative
=> Something I didn't expect at all went wrong (system error - unanticipated exception - externally forced termination e.g. kill -9
)
Negative
=> 我根本没想到的事情出错了(系统错误 - 意外异常 - 外部强制终止,例如kill -9
)
(values greater than 128 are actually negative, if you regard them as 8-bit signed binary, or twos complement)
(大于 128 的值实际上是负数,如果您将它们视为 8 位有符号二进制或二进制补码)
There's a load of good standard exit-codes here
这里有很多很好的标准退出代码
回答by Premraj
System.exit(system call)
terminates the currently running Java virtual machine by initiating its shutdown sequence. The argument serves as a status code.
System.exit(system call)
通过启动其关闭序列来终止当前运行的 Java 虚拟机。该参数用作状态代码。
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
在Java 上阅读更多内容
On Unix and Linux systems, 0
for successful executions and 1
or higher for failed executions.
在 Unix 和 Linux 系统上,0
成功执行1
或更高级别的失败执行。
回答by Lova Chittumuri
Here is the answer.
这是答案。
System.exit(0);// normal termination - Successful - zero
System.exit(-1);//Exit with some Error
System.exit(1);//one or any positive integer // exit with some Information message
回答by androminor
System.exit(0)by convention, a nonzero status code indicates abnormal termination.
System.exit(0)按照惯例,非零状态码表示异常终止。
System.exit(1)-It means termination unsuccessful due to some error
System.exit(1)- 表示由于某些错误终止不成功
回答by nguyên
As others answer 0
meaning success, otherwise.
正如其他人的回答0
意味着成功,否则。
If you using bat file (window) System.exit(x)
will effect.
如果你使用bat文件(窗口)System.exit(x)
会影响。
Code java (myapp):
代码java(myapp):
if (error < 2){
help();
System.exit(-1);
}
else{
doSomthing();
System.exit(0);
}
}
}
bat file:
bat文件:
java -jar myapp.jar
if %errorlevel% neq 0 exit /b %errorlevel%
rem -- next command if myapp is success --
回答by nomad
A good gotcha is any error code > 255 will be converted to error code % 256. One should be specifically careful about this if they are using a custom error code > 255 and expecting the exact error code in the application logic. http://www.tldp.org/LDP/abs/html/exitcodes.html
一个很好的问题是任何错误代码 > 255 都将转换为错误代码 % 256。如果他们使用自定义错误代码 > 255 并期望应用程序逻辑中的确切错误代码,则应该特别注意这一点。 http://www.tldp.org/LDP/abs/html/exitcodes.html
回答by LEGEND MORTAL
class calc{
public static void main(String args[])
{
int a, b, c;
char ch;
do{
Scanner s=new Scanner(System.in);
System.out.print("1. Addition\n");
System.out.print("2. Substraction\n");
System.out.print("3. Multiplication\n");
System.out.print("4. Division\n");
System.out.print("5. Exit\n\n");
System.out.print("Enter your choice : ");
ch=s.next().charAt(0);
switch (ch)
{
case '1' :
Addition chose1=new Addition();
chose1.add();
break;
case '2' :
Substraction chose2=new Substraction();
chose2.sub();
break;
case '3' :
Multiplication chose3= new Multiplication();
chose3.multi();
break;
case '4' :
Division chose4=new Division();
chose4.divi();
break;
case '5' :
System.exit(0);
break;
default :
System.out.print("wrong choice!!!");
break;
}
System.out.print("\n--------------------------\n");
}while(ch !=5);
}
}
}
In the above code when its System.exit(0); and when i press case 5 it exits properly but when i use System.exit(1); and press case 5 it exits with error and again when i try with case 15 it exits properly by this i got to know that, when ever we put any int inside argument it specifies that, it take the character from that position i.e if i put (4) that it means take 5th character from that string if its (3) then it means take 4th character from that inputed string
在上面的代码中,当其 System.exit(0); 当我按下 case 5 时,它会正确退出,但是当我使用 System.exit(1) 时;并按 case 5 它以错误退出,当我再次尝试使用 case 15 时,它会通过这个正确退出我知道,当我们在参数中放入任何 int 时,它指定,它从那个位置获取字符,即如果我把(4) 表示从该字符串中取第 5 个字符,如果 (3) 则表示从该输入字符串中取第 4 个字符
回答by Purushottam Sadh
exit(0)
generally used to indicate successful termination. exit(1)
or exit(-1)
or any other non-zero value indicates unsuccessful termination in general.
exit(0)
一般用于表示成功终止。exit(1)
或exit(-1)
或任何其他非零值通常表示不成功的终止。