This static System.exit()
function ceases
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
.
public static void exit(int status)
status
: This is a mode of termination.
status
> 0: stipulates abnormal termination.status
< 0: stipulates abnormal termination.status
= 0: normal termination of program execution.This function does not return any value.
It returns a SecurityException
when it does not allow an exit on a specified status
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()");}}
status
value 0
is used. It signals 1,-1
as our termination values, the programmer has the advantage of setting some errors.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 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);}}}
try{}
block, we use the FileReader()
method to read a data.csv
file.catch(){}
block ,it throws the File not found
exception. Then it prints it on the console.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);}}}
try{}
block, we use the FileReader()
method to read a Olivetti.csv
file.catch(){}
block, we print a File not found
exception.-1