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

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.