What is the System.exit() function in Java?

Overview

This static System.exit() function ceases JVMJava Virtual Machine’s execution.

The status argument value tells us about the termination behavior.

Note: The status argument value calls the class runtime method, Runtime.getRuntime().exit(n), to get the current status.

Here, n can have the values -1, 0, or 1.

Syntax


public static void exit(int status)

Parameters

status : This is a mode of termination.

  • status > 0: stipulates abnormal termination.
  • status < 0: stipulates abnormal termination.
  • status = 0: normal termination of program execution.

Return value

This function does not return any value. It returns a SecurityException when it does not allow an exit on a specified status code.

Code

The code snippets below illustrate how the System.exit() function works on different status values.

status = 0

Let’s consider exit(0), where JVM will terminate at 0. This means a normal termination of the program:

// Load libraries
import java.util.*;
import java.lang.*;
// Main Class
public class EdPresso {
// main method
public static void main(String[] args)
{
int num[] = {12, 9, 13, 24, 35, 36, 7, 18, 4, 3};
// iterator for num array
for (int i = 0; i < num.length; i++) {
if (num[i] <= 10) {
System.out.println("--- exit(0) method called ---");
System.exit(0); // Terminate java virtual machines
}
else{
if(num[i] % 2 ==0){
System.out.println("num["+i+"] = " + num[i]);
}
else
{
System.out.println("num["+i+"] = " + num[i]);
System.out.println("--- exit(-1) method called ---");
System.exit(-1);
}
}
}
System.out.println("Code successfully ending the main()");
}
}

Explanation

  • Line 14: The program exits successfully. That’s why the status value 0 is used. It signals JVMJava Virtual Machine to mark the program execution complete.
  • Line 24: We can also use non-zero values for abnormal program termination. By using 1,-1 as our termination values, the programmer has the advantage of setting some errors.
  • Line 29: We try to print that the main function terminates successfully and does not execute because of exit().

status = 1

Now let’s consider exit(1), where JVM will terminate at 1. This means an abnormal termination of the program:

// Load libraries
import java.util.*;
import java.lang.*;
import java.io.*;
// Main Class
public class EdPresso {
// main method
public static void main(String[] args) throws Exception {
try {
BufferedReader br = new BufferedReader(new FileReader("data.csv"));
int i;
while ((i = br.read()) != -1) {
System.out.print((char) i);
}
} catch (IOException e) {
System.out.println("Exception: " + e);
System.out.println("\nJVM Ceased at +1");
System.exit(1);
}
}
}

Explanation

  • Line 12: In the try{} block, we use the FileReader() method to read a data.csv file.
  • Line 18: If an exception comes in the catch(){} block ,it throws the File not found exception. Then it prints it on the console.
  • Line 20: If an exception comes, it means we did not find a specified file and JVM should terminate with an error value > 0.

status = -1

Now let’s consider exit(-1), where JVM will terminate at -1. This means an abnormal termination of the program:

// Load libraries
import java.util.*;
import java.lang.*;
import java.io.*;
// Main Class
public class EdPresso {
// main method
public static void main(String[] args) throws Exception {
try {
// trying to read a csv file
BufferedReader buffer = new BufferedReader(new FileReader("Olivetti.csv"));
System.out.println("File is ready");
}
catch (IOException e) {
System.out.println(e + "\nJVM Ceased at -1");
// JVM abnormal termination
System.exit(-1);
}
}
}

Explanation

  • Line 12: In the try{} block, we use the FileReader() method to read a Olivetti.csv file.
  • Line 16: If an exception comes in the catch(){} block, we print a File not found exception.
  • Line 18: If an exception comes, it means that we did not find a specified file and JVM should terminate (halt the program execution) with an errored value < 0, that is, -1

Free Resources