This blog will elaborate on using and implementing the “System.exit()” method in Java.
What is “System.exit()” in Java?
The “exit()” method of the “System” class terminates the current Java virtual machine executing on the system normally or abnormally based on its(method) parameter.
Syntax
In the above syntax, “int status” refers to the termination modes as follows:
- “status = 0”: It normally terminates the program execution.
- “status > 0” or “status < 0”: results in abnormal termination of the program.
Example 1: Applying “System.exit()” in Java With Status “0”
In this example, the “System.exit()” method can be applied to normally terminate the program’s execution by iterating through an integer array:
In the above lines of code, apply the following steps:
- Firstly, create an integers array named “arrayNum[ ]”.
- In the next step, apply the “for” loop and the associated “length” property to iterate along the array elements.
- Within the loop, apply a condition such that upon the satisfied/met condition in the “if” statement, the “System.exit()” method becomes invoked, thereby terminating the program normally.
- Algorithm: The array elements iterate from start to the end considering the placed condition and upon the first occurrence leading to the unsatisfied condition, i.e., “8” in the array, the program terminates normally.
- In all the other cases, the “else” condition comes into effect.
Output
In this output, it can be seen that upon iterating the array elements, the “System.exit()” method is invoked upon the first satisfied occurrence accordingly.
Before heading to the next example, include the following library to work with the “input” and “output” streams:
Example 2: Applying “System.exit()” in Java With Status “-1”
The “BufferedReader” reads from the “character-based stream” and the “try/catch” statement performs the functionalities and handles the encountered exceptions while execution, respectively.
In this particular example, these approaches can be implemented combined with the “System.exit()” method to abnormally terminate the program’s execution by displaying an exception:
In this code block:
- First of all, in the “try” block, create a “BufferedReader” object named “readFile” using the “new” keyword and the “BufferedReader()” constructor, respectively.
- Also, create a file reader to read the specified file.
- Now, in the “catch” block, cope with the encountered exception in the “try” block and halt the program execution in the case of encountered exception.
- This leads to abnormally terminating the program’s execution via the specified “-1” as method’s, i.e., “System.exit()” parameter.
Output
In this outcome, it can be implied that the specified file cannot be located and read and so the “FileNotFoundException” is logged on the console and the program terminates abnormally.
Conclusion
The “exit()” method of the “System” class terminates the current Java virtual machine running on the system normally or abnormally based on its parameter “0” or “1/-1”, respectively. This blog demonstrated the implementation of the “System.exit()” method in different scenarios.