Home »
Java programming language
Java System class exit() method with example
System class exit() method: Here, we are going to learn about the exit() method of System class with its syntax and example.
Submitted by Preeti Jain, on September 16, 2019
System class exit() method
- exit() method is available in java.lang package.
- exit() method is used to exit the currently running JVM (Java Virtual Machine).
- exit() method is a static method, it is accessible with the class name too.
- exit() method may be thrown various type of exception and the description of exception are given below,
SecurityException: If a particular method checkExit() does not allow exit with the given exit_status when security manager exists in the method.
Syntax:
public static void exit (int exit_status);
Parameter(s):
- exit_status – represents the stages or level of termination of JVM, and here, the non-zero value represents the abnormal termination of JVM.
Return value:
The return type of this method is void, it does not return any value.
Example:
// Java program to demonstrate the example of
// exit() method of System Class
public class ExitMethod {
public static void main(String[] args) {
// declaring an array
int array[] = {
10,
20,
30,
40,
50
};
for (int i = 0; i < array.length; ++i) {
if (array[i] < 60) {
System.out.println("Element at index" + " " + i + " is " + array[i]);
} else {
System.out.println("We are exiting JVM normally");
System.exit(0);
}
}
}
}
Output
E:\Programs>javac ExitMethod.java
E:\Programs>java ExitMethod
Element at index 0 is 10
Element at index 1 is 20
Element at index 2 is 30
Element at index 3 is 40
Element at index 4 is 50