Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we have exit codes in Java?

Tags:

java

exit-code

In Java, we use System.exit(int) to exit the program. The reason for an "exit value" in C was that the exit value was used to check for errors in a program. But in Java, errors are reflected by an Exception being thrown, thus they can be handled easily. So why do we have exit values in Java at all?

like image 659
bane Avatar asked Dec 26 '12 13:12

bane


1 Answers

exit values are returned to the calling program e.g. the shell. An Exception cannot be caught by an external program.

BTW When you throw an Exception it is caught by that thread or that thread dies, the finally blocks are still called for that thread. When you call System.exit(), all threads stop immediately and finally blocks are not called.

like image 182
Peter Lawrey Avatar answered Oct 14 '22 08:10

Peter Lawrey