System.clearProperty() in Java

System.clearProperty(): This method is available in java.lang.System class of Java.

Syntax:

String java.lang.System.clearProperty(String key)

This method takes one argument of type String as its parameter. This method removes the system property indicated by the specified key.

Parameters: One parameter is required for this method.

key: the name of the system property to be removed.

Returns: the previous string value of the system property, or null if there was no property with that key.

Throws:

1. SecurityException - if a security manager exists and its checkPropertyAccess method doesn't allow access to the specified system property.

2. NullPointerException - if the key is null.

3. IllegalArgumentException - if the key is empty.

Approach 1: When no exceptions.

Java

public class SystemclearProperty {
    public static void main(String[] args) {

        String key = "os.name";
        System.out.println(System.clearProperty(key));
    }
}

Output:

Windows 10


Approach 2: NullPointerException 

Java

public class SystemclearProperty {
    public static void main(String[] args) {

        String key = null;
        System.out.println(System.clearProperty(key));
    }
}


Output:

Exception in thread "main" java.lang.NullPointerException: key can't be null at java.base/java.lang.System.checkKey(System.java:960) at java.base/java.lang.System.clearProperty(System.java:949)


Approach 3: IllegalArgumentException

Java

public class SystemclearProperty {
    public static void main(String[] args) {

        String key = "";
        System.out.println(System.clearProperty(key));
    }
}


Output:

Exception in thread "main" java.lang.IllegalArgumentException: key can't be empty at java.base/java.lang.System.checkKey(System.java:963) at java.base/java.lang.System.clearProperty(System.java:949)


No comments:

Post a Comment