Exiting a Java program

When a Java program has gone through all code it exits automatically by returning zero if all has gone well.
In some situations you might want to exit the program in the middle of a program, and this is done by calling the static exit method of the System class.
The exit method takes an int as argument so if you want to exit the program normally you write:

Should you want to exit because of for example some error has occured you normally specify a negative number as the argument.
You could of course specify any error code you like so long it differs from zero but normally -1 is used:

Search for more Java info on this site here:
Custom Search

Please type any questions here.