Home » Java programming language

Java SecurityManager checkExit() method with example

SecurityManager Class checkExit() method: Here, we are going to learn about the checkExit() method of SecurityManager Class with its syntax and example.
Submitted by Preeti Jain, on December 15, 2019

SecurityManager Class checkExit() method

  • checkExit() method is available in java.lang package.
  • checkExit() method calls checkPermission with RuntimePermission("exitVM" + "exit_status") it exits successfully when the given argument value is 0 otherwise it exits unsuccessfully when the given argument value is not equal to 0.
  • checkExit() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • checkExit() method may throw an exception at the time of thread termination.
    SecurityException – This exception may throw when the calling thread is not allowed to stop the JVM with the given status and it is called for the current security manager by using the exit() method of Runtime.

Syntax:

    public void checkExit(int exit_status);

Parameter(s):

  • int exit_status – represents the exit status of the thread.

Return value:

The return type of this method is void, it returns nothing.

Example:

// Java program to demonstrate the example 
// of void checkExit(int exit_status)
// method of SecurityManager 

public class CheckExit extends SecurityManager {
    // Override checkExit() of SecurityManager
    public void checkExit(int exit_status) {
        throw new SecurityException("Restricted.. ");
    }

    public static void main(String[] args) {
        // By using setProperty() method is to set the policy property 
        // with security manager
        System.setProperty("java.security.policy", "file:/C:/java.policy");

        // Instantiating a SecurityManager object
        SecurityManager smgr = new SecurityManager();

        // By using setSecurityManager() method is to set the
        // security manager
        System.setSecurityManager(smgr);

        // By using checkExit(4) method is to exit
        // process with a integer value
        smgr.checkExit(4);

        // Display the message
        System.out.println("Not Restricted..");
    }
}

Output

Not Restricted..


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.