Java System.exit() – Examples

In this tutorial, we will learn about the Java System.exit() function, and learn how to use this function to exit out of the currently running Java application, with the help of examples.

exit(int status)

System.exit() terminates the currently running Java Virtual Machine. You can use this method to terminate the Java program, that is currently running.

The status parameter can specify if the termination is intended or abnormal.

Syntax

The syntax of exit() function is

</>
Copy
exit(int status)

where

ParameterDescription
statusThe status represents exit status.

Returns

The function returns void.

Example 1 – exit() – Normal Exit

In this example, we will exit out of the running application using exit() method. To tell the Java Runtime Environment, that this exit is a normal exit, we pass the status 0 as argument to exit().

Java Program

</>
Copy
public class Example {
	public static void main(String args[]) {
		System.out.println("Hello World!");
		System.exit(0);
		System.out.println("Never Runs!");
	}
}

Output

Hello World!

Example 2 – exit() – Abnormal Exit

In this example, we will pass the status -1 to exit. Any non zero integer status specifies that the exit is not normal.

Java Program

</>
Copy
public class Example {
	public static void main(String args[]) {
		System.out.println("Hello World!");
		System.exit(-1);
		System.out.println("Never Runs!");
	}
}

Output

Hello World!

Example 3 – exit() – Avoid Exit using SecurityManager

In this example, we will use Security Manager to avoid exit with a specified exit status code.

We print numbers from 1 to 10. But, when if becomes 3, we are calling exit(1). The status code we are passing is 1, and for this status code we are throwing SecurityException in checkExit() method of SecurityManager.

Java Program

</>
Copy
public class Example {
	public static void main(String args[]) {
		System.setSecurityManager(new SecurityManager() {
			@Override
			public void checkExit(int status) {
				if(status == 1) {
					throw new SecurityException("Not allowed.");
				} else {
					System.out.println("Exit!");
				}
			}
		});

		for (int i = 1; i <= 10; i++) { 
			if (i == 3) { 
				try {
					System.exit(1);
				} catch (SecurityException e) {
					System.out.println("Exit not allowed by Security Manager for this status code!");
				} 
			} 
			System.out.println("i is :" +i); 
		} 
		System.out.println("End of Program");  
	}
}

Output

i is :1
i is :2
Exit not allowed by Security Manager for this status code!
i is :3
i is :4
i is :5
i is :6
i is :7
i is :8
i is :9
i is :10
End of Program

Now, let us pass a different status code, and check the behavior of exit() method.

Java Program

</>
Copy
public class Example {
	public static void main(String args[]) {
		System.setSecurityManager(new SecurityManager() {
			@Override
			public void checkExit(int status) {
				if(status == 1) {
					throw new SecurityException("Not allowed.");
				} else {
					System.out.println("Exit!");
				}
			}
		});

		for (int i = 1; i <= 10; i++) { 
			if (i == 3) { 
				try {
					System.exit(2);
				} catch (SecurityException e) {
					System.out.println("Exit not allowed by Security Manager for this status code!");
				} 
			} 
			System.out.println("i is :" +i); 
		} 
		System.out.println("End of Program");  
	}
}

Output

i is :1
i is :2
Exit!

Conclusion

In this Java Tutorial, we have learnt the syntax of Java System.exit() function, and also learnt how to use this function with the help of examples.