What is the System.exit() function in Java?
Overview
This static System.exit() function ceases
The status argument value tells us about the termination behavior.
Note: The
status argument valuecalls 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 librariesimport java.util.*;import java.lang.*;// Main Classpublic class EdPresso {// main methodpublic static void main(String[] args){int num[] = {12, 9, 13, 24, 35, 36, 7, 18, 4, 3};// iterator for num arrayfor (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
statusvalue0is used. It signals to mark the program execution complete.JVM Java Virtual Machine - Line 24: We can also use non-zero values for abnormal program termination. By using
1,-1as our termination values, the programmer has the advantage of setting some errors. - Line 29: We try to print that the
mainfunction terminates successfully and does not execute because ofexit().
status = 1
Now let’s consider exit(1), where JVM will terminate at 1. This means an abnormal termination of the program:
// Load librariesimport java.util.*;import java.lang.*;import java.io.*;// Main Classpublic class EdPresso {// main methodpublic 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 theFileReader()method to read adata.csvfile. - Line 18: If an exception comes in the
catch(){}block ,it throws theFile not foundexception. 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 librariesimport java.util.*;import java.lang.*;import java.io.*;// Main Classpublic class EdPresso {// main methodpublic static void main(String[] args) throws Exception {try {// trying to read a csv fileBufferedReader 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 terminationSystem.exit(-1);}}}
Explanation
- Line 12: In the
try{}block, we use theFileReader()method to read aOlivetti.csvfile. - Line 16: If an exception comes in the
catch(){}block, we print aFile not foundexception. - 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