Java 的 System.exit(0);vs C++ 返回 0;
当我们在学校学习C++时,我们的教授会告诉我们在main函数的最后一行代码写return 0;
,这被认为是一种很好的编程习惯.
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.
在Java中,我意识到有些人在main方法的最后一行写了System.exit(0);
.
In Java, I realise some people writes System.exit(0);
at the last line of the main method.
但是,在 C++ 中,如果我使用 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.
我的问题:Java 的 System.exit(0);
是否类似于 C++ 的 return 0;
?(或者是不是类似于C++的exit(0)
)
My question: Is Java's System.exit(0);
similar to C++'s return 0;
? (Or is it similar to C++'s exit(0)
)
在 java 中使用 System.exit(0)
是不好的做法(即:写在 main 方法的最后一行)?
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)?
推荐答案
Java的System.exit(0)
就像C++的exit(0)
一样
Java's System.exit(0)
is like C++'s exit(0)
in that
- 它终止进程
- 可以从程序中的任何地方调用.
后一点使它成为非结构化"的.学术类型倾向于皱眉的控制流结构.(在这种情况下,他们有一个很好的理由:如果一个函数通过异常(或老式的返回码)报告失败,调用者可以从错误中恢复,但是 exit
烧毁那座桥.当然,有时确实需要将错误视为致命错误.)
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.)
在 C++ 中,return 0
等价于 exit(0)
如果它在 main
函数中.(在其他函数中,没有什么特别的意思.)
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++ 和 Java 的相关区别在于 main
的返回类型.
The relevant different between C++ and Java here is the return type of main
.
- 在 C++ 中,
main
必须返回int
.通常,这意味着它必须有一个return
语句,但是 C++ 标准使main
成为一个带有隐含return 的特殊情况0;
不写就结束. - 在 Java 中,
main
必须返回void
.
- In C++,
main
must returnint
. Normally, this would mean that it must have 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++ 中,为了与所有其他非 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.
在Java 中,您不能return
main
的退出代码,因为它是一个void
函数.因此,如果要显式指定退出代码,则必须使用 System.exit
.
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
.
用 System.exit(0)
结束每个 Java main
函数并没有错误,但这样做并不符合习惯,除非你有一个类似的结构
It's not wrong to 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
public static void main(String[] args) {
try {
//... do the work
System.exit(0);
} catch (Throwable t) {
//... report the error
System.exit(1);
}
}
相关文章