Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 (I.e.: writing it in last line of main method)?

like image 693
user3437460 Avatar asked Jun 25 '14 06:06

user3437460


People also ask

Is Exit 0 and return 0 the same?

Difference between exit(0) and return 0 in C++:-When exit(0) is used to exit from program, destructors for locally scoped non-static objects are not called. But destructors are called if return 0 is used.

What is difference between Exit 0 and Exit 1 in C?

exit(0) indicates that the program terminated without errors. exit(1) indicates that there were an error. You can use different values other than 1 to differentiate between different kind of errors.

What is the difference between return and System Exit 0 in Java?

Note: The work of both return 0 and System. exit(0) is the same as the difference in the return type of the main() functions.

What is the use of System Exit 0?

exit(0) : Indicates successful termination. exit(1) or exit(-1) or any non-zero value – indicates unsuccessful termination.


2 Answers

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);
   }
}
like image 131
dan04 Avatar answered Sep 29 '22 12:09

dan04


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
}
like image 25
Rahul Tripathi Avatar answered Sep 29 '22 14:09

Rahul Tripathi