Java的System.exit(0); vs C ++ return 0; [英] Java's System.exit(0); vs C++ return 0;

查看:242
本文介绍了Java的System.exit(0); vs C ++ return 0;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们在学校学习C ++时,我们的教授会告诉我们在主函数的最后一行代码中写 return 0; ,它被认为是好的编程实践。



在Java中,我意识到有些人在最后一行写了 System.exit(0);



但是,在C ++中,如果我使用 exit(0); 我的教授,因为(在学校)为程序编程,我们期望让程序流到主结束,让程序自然停止。



我的问题:是Java的 System.exit(0); 类似于C ++的 return 0; ? (或者类似于C ++的 exit(0)



是不好的做法,使用 System.exit(0)在java中是不必要的(即在main方法的最后一行中写它)?

Java的 System.exit(0)就像C ++的 exit(0)


$ b

  • 它会终止进程

  • >。



后一点使其成为非结构化控制流结构,学术类型往往会皱眉。 (在这种情况下,他们有一个很好的理由:如果一个函数通过异常(或老式的返回代码)报告故障,调用者可能从错误中恢复,但 exit



在C ++中, return 0 等效于 exit(0) 如果它在 main



C ++和Java之间的不同之处在于返回类型 main




  • 在C ++中, main 必须返回 int 。通常,这意味着它必须有返回语句,但C ++标准使 main c>一个特殊情况,隐含返回0; 作为结束,如果不写一个。

  • code> main 必须返回 void



在C ++中,通常在 main 中写入 return 语句,即使它在技术上是多余的,与所有其他非< - > void 函数。



在Java中, $ c> return 退出代码 main ,因为它是一个 void 函数。所以,如果你想明确指定一个退出代码,你必须使用 System.exit



结束每个具有 System.exit(0)的Java main 除非你有像

 这样的结构public static void main(String [] args){
try {
// ...做工作
System.exit(0);
} catch(Throwable t){
// ...报告错误
System.exit(1);
}
}


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.

In Java, I realise some people writes System.exit(0); at the last line of the main method.

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.

My question: Is Java's System.exit(0); similar to C++'s return 0; ? (Or is it similar to C++'s 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)?

解决方案

Java's System.exit(0) is like C++'s exit(0) in that

  • It terminates the process
  • It can be called from anywhere in 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.)

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.)

The relevant different between C++ and Java here is the return type of main.

  • In C++, main must return int. Normally, this would mean that it must have a return statement, but the C++ standard makes main a special case with an implied return 0; as the end if you don't write one.
  • In Java, main must return 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.

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.

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);
   }
}

这篇关于Java的System.exit(0); vs C ++ return 0;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆