Java 的 System.exit(0); vs C++返回0;
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24401621/
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
Java's System.exit(0); vs C++ return 0;
提问by user3437460
When we learn C++ in school, our professor will tell us to write return 0;
at the last line of codes in the main function and it is considered as a good programming practice.
当我们在学校学习C++时,我们的教授会告诉我们写return 0;
在main函数的最后一行代码,这被认为是一种很好的编程习惯。
In Java, I realise some people writes System.exit(0);
at the last line of the main method.
在 Java 中,我意识到有些人写System.exit(0);
在 main 方法的最后一行。
However, in C++, if I use exit(0);
I get penalized by my professor, because (in school) for procedural programming, we are expected to let the program flow till the end of main and let the program stop naturally.
但是,在 C++ 中,如果我使用exit(0);
我会受到教授的惩罚,因为(在学校)对于过程式编程,我们希望让程序运行到 main 结束并让程序自然停止。
My question: Is Java's System.exit(0);
similar to C++'s return 0;
? (Or is it similar to C++'s exit(0)
)
我的问题:JavaSystem.exit(0);
与 C++ 相似return 0;
吗?(或者它类似于 C++ 的exit(0)
)
Is it bad practice to use System.exit(0)
in java when it is unnecessary (I.e.: writing it in last line of main method)?
System.exit(0)
在不需要时在java中使用是不是不好的做法(即:在main方法的最后一行编写它)?
回答by Elliott Frisch
Yes. Java's System.exit(0);
is a direct analogue because the prototypical Java main
method is defined as
是的。JavaSystem.exit(0);
是一个直接的类比,因为原型 Javamain
方法被定义为
public static void main(String[] args) {
}
So it literally cannot return
an int
(although it can simply return;
). In C++ you could exit(0);
which is also equivalent.
所以,它的字面不能return
的int
(虽然它可以简单地return;
)。在 C++ 中,你可以exit(0);
,这也是等价的。
回答by Rahul Tripathi
In Java System.exit(0),System.exit(1),System.exit(-1) is used to know if the execution of the program went good or bad. 0
is used when execution went fine. 1, -1, whatever != 0
when some error occurred, you can use different values for different kind of errors.
在 Java System.exit(0),System.exit(1),System.exit(-1) 中用于知道程序执行的好坏。0
在执行顺利时使用。1, -1, whatever != 0
当发生某些错误时,您可以对不同类型的错误使用不同的值。
You can also say it like this:
你也可以这样说:
0mean everything Okay
0表示一切正常
1means something which I expecting to go wrong went wrong
1表示我预期会出错的事情出错了
-1means something which I didn't expect went wrong
-1表示我没想到出了问题
The Java Docsays:
在Java文档说:
The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination.
参数用作状态代码;按照惯例,非零状态代码表示异常终止。
Is Java's System.exit(0); similar to C++'s return 0; ?
是Java的System.exit(0); 类似于 C++ 的 return 0; ?
Yes they both are similar.
是的,它们都很相似。
Is it bad practice to use System.exit(0) in java when it is unnecessary
不必要时在 java 中使用 System.exit(0) 是不好的做法吗
I would not say it is a bad practice but yes you should be aware of when to use it and where to use it. You can use it to throw an error for a command line tool via a exit code instead of throwing an exception. System.exit(0)
terminates the JVM so you should be very much sure where to use that.
我不会说这是一种不好的做法,但是是的,您应该知道何时使用它以及在哪里使用它。您可以使用它通过退出代码为命令行工具抛出错误,而不是抛出异常。System.exit(0)
终止 JVM,因此您应该非常确定在哪里使用它。
An example for that:
一个例子:
try {
runTheWholeApplication();
System.exit(0); // Signifying the normal end of your program
} catch (Throwable t) {
reportErrorSomehow(t);
System.exit(1); // Signifying that there is an error
}
回答by fredoverflow
When we learn C++ in school, our professor will tell us to write
return 0;
at the last line of codes in the main function and it is considered as a good programming practice.
当我们在学校学习C++时,我们的教授会告诉我们写
return 0;
在main函数的最后一行代码,这被认为是一种很好的编程习惯。
That's debatable. Has he also told you that return 0;
is implicit inside main
? Personally, I never write the redundant return 0;
inside main
, and I almost never see it in other people's code either.
这是有争议的。他有没有告诉过你那return 0;
是内隐的main
?就我个人而言,我从来没有在return 0;
里面写过多余的东西main
,我也几乎从来没有在其他人的代码中看到过。
回答by Joop Eggen
The original operating system Unix heavily relied on command line scripts using and combining many small programs. A return code of a program was used for the control flow. Return code 0 meaning everything okay, non-zero for errors.
最初的操作系统 Unix 严重依赖于使用和组合许多小程序的命令行脚本。程序的返回码用于控制流。返回代码 0 表示一切正常,错误时非零。
So the Cint main(...)
is a direct mapping, and return 0;
at the end appropiate. An exit there is like throwing an exception, bypassing any return. It also disables calling the main from another program.
所以Cint main(...)
是一个直接映射,return 0;
最后是合适的。那里的退出就像抛出异常,绕过任何返回。它还禁止从另一个程序调用 main。
In Javapublic void main(...)
this was simplified, and 0 is implicit. System.exit(0);
has the same bad assumption that no other program calls main
. Though in general void
mirrored the 99% usage, now System.exit(...);
is inevitable for a non-zero error code.
在Java 中,public void main(...)
这被简化了,0 是隐式的。System.exit(0);
具有相同的错误假设,即没有其他程序调用main
. 虽然通常void
反映了 99% 的使用率,但现在System.exit(...);
对于非零错误代码来说是不可避免的。
This must also be seen as a more general design decision; C heavily relies on return codes of functions, which makes the control flow difficult or error prone, whereeas Java introduced the usage of Exceptions as possible obligatory control flow, with a freely chooseable catch. Nowadays an alternative to System.exit(13)
might be throw new IllegalArgumentException("Unlucky");
.
这也必须被视为更一般的设计决策;C 严重依赖函数的返回代码,这使得控制流变得困难或容易出错,而 Java 引入了使用异常作为可能的强制性控制流,并具有可自由选择的捕获。如今,System.exit(13)
可能是throw new IllegalArgumentException("Unlucky");
.
回答by dan04
Java's System.exit(0)
is like C++'s exit(0)
in that
Java的System.exit(0)
就像C++的exit(0)
那样
- It terminates the process
- It can be called from anywherein the program.
- 它终止进程
- 它可以从程序的任何地方调用。
The latter point makes it an “unstructured” control-flow construct that academic types tend to frown on. (In this case, they've got a good reason: If a function reports a failure via an exception (or old-fashioned return code), it's possible for the caller to recover from the error, but exit
burns that bridge. Of course, sometimes errors do need to be treated as fatal.)
后一点使其成为学术类型倾向于不赞成的“非结构化”控制流结构。(在这种情况下,他们有一个很好的理由:如果一个函数通过异常(或老式的返回码)报告失败,调用者有可能从错误中恢复,但会exit
烧毁那座桥梁。当然,有时确实需要将错误视为致命错误。)
In C++, return 0
is equivalent to exit(0)
if it's in the main
function. (In other functions, it doesn't mean anything special.)
在 C++ 中,return 0
等价于exit(0)
如果它在main
函数中。(在其他函数中,它并不意味着什么特别的。)
The relevant different between C++ and Java here is the return type of main
.
这里 C++ 和 Java 的相关区别是main
.
- In C++,
main
must returnint
. Normally, this would mean that it musthave areturn
statement, but the C++ standard makesmain
a special case with an impliedreturn 0;
as the end if you don't write one. - In Java,
main
must returnvoid
.
- 在 C++ 中,
main
必须返回int
. 通常,这意味着它必须有一个return
语句,但是 C++ 标准会提出main
一种特殊情况,return 0;
如果您不编写,则隐含作为结束。 - 在 Java 中,
main
必须返回void
.
In C++, it's common to write return
statements in main
, even though it's technically redundant, for stylistic consistency with all the other non-void
functions in your program.
在 C++ 中,为了与程序中所有其他非函数保持风格一致,return
在 中编写语句是很常见的main
,即使它在技术上是多余的void
。
In Java, you can't return
the exit code from main
because it's a void
function. So, if you want to explicitly specify an exit code, you have to use System.exit
.
在 Java 中,您不能return
退出代码,main
因为它是一个void
函数。因此,如果要明确指定退出代码,则必须使用System.exit
.
It's not wrongto end every Java main
function with System.exit(0)
, but it's just not idiomatic to do so unless you've got a construct like
用 结束每个 Java函数并没有错,但这样做并不习惯,除非你有这样的结构main
System.exit(0)
public static void main(String[] args) {
try {
//... do the work
System.exit(0);
} catch (Throwable t) {
//... report the error
System.exit(1);
}
}