Java

System.exit() in Java

In Java programming, there can be certain instances where the developer needs to halt the program execution from time to time. More specifically, in the case of complex codes where the exceptions are relatively more probable. In such situations, the “System.exit()” method is effective in streamlining the logical exceptions in the code as well as saving time at the programmer’s end.

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

public static void exit(int status)

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:

public class exit {
 public static void main(String[] args) {
  int arrayNum[] = {2, 6, 8, 10, 12};
for (int i = 0; i= 8) {
System.out.println("exit(0) method invoked!");
System.exit(0);
}
else {
System.out.println("arrayNum["+i+"] = " +
arrayNum[i]);
}
}
}}

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:

import java.io.*;

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:

public class exit2 {
publicstaticvoidmain(String[] args) {
try {
BufferedReaderreadFile = newBufferedReader(newFileReader("abc.txt"));
System.out.println("Valid File");
}
catch (Exception e) {
System.out.println(e + "\nTerminated at -1");
System.exit(-1);
}
}}

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.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.