Java's System.exit(0); vs C++ return 0;

亡梦爱人 提交于 2019-12-20 17:39:25

问题


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


回答1:


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



回答2:


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.

You can also say it like this:

0 mean everything Okay

1 means something which I expecting to go wrong went wrong

-1 means something which I didn't expect went wrong

The Java Doc says:

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; ?

Yes they both are similar.

Is it bad practice to use System.exit(0) in java when it is unnecessary

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.

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
}



回答3:


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.

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.




回答4:


Yes. Java's System.exit(0); is a direct analogue because the prototypical Java main method is defined as

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.




回答5:


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.

So the C int 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.

In Java public 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.

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



来源:https://stackoverflow.com/questions/24401621/javas-system-exit0-vs-c-return-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!