Java's System.exit(0); vs C++ 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.

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 (Ie: 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);
       }
    }
    

    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
    }
    

    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.

    链接地址: http://www.djcxy.com/p/96386.html

    上一篇: 将Java的lambda表达式与Swift的函数类型进行比较

    下一篇: Java的System.exit(0); vs C ++返回0;