java.net.NetPermission
This class is for various network permissions. A NetPermission contains a name (also referred to as a "target name") but no actions list; you either have the named permission or you don't.
The target name is the name of the network permission (see below). The naming convention follows the hierarchical property naming convention. Also, an asterisk may appear at the end of the name, following a ".", or by itself, to signify a wildcard match.
Methods of NetPermission
1. NetPermission.NetPermission(String name)
java.net.NetPermission.NetPermission(String name)
This method takes one argument. This method creates a new NetPermission with the specified name. The name is the symbolic name of the NetPermission, such as"setDefaultAuthenticator", etc. An asterisk may appear at the end of the name, following a ".", or by itself, to signify a wildcard match.
Parameters: One parameter is required for this method.
name: the name of the NetPermission.
Throws:
1. NullPointerException - if the name is null.
2. IllegalArgumentException - if the name is empty.
Approach a: When no exception
Java
package com.NetPermission;import java.net.NetPermission;public class NetPermission1 {public static void main(String[] args) {String name = "hello";NetPermission netPermission = new NetPermission(name);System.out.println(netPermission);}}
Output:
("java.net.NetPermission" "hello")
Approach b: NullPointerException
Java
package com.NetPermission;import java.net.NetPermission;public class NetPermission1 {public static void main(String[] args) {String name = null;NetPermission netPermission = new NetPermission(name);System.out.println(netPermission);}}
Output:
Exception in thread "main" java.lang.NullPointerException: name can't be null at java.base/java.security.BasicPermission.init(BasicPermission.java:89) at java.base/java.security.BasicPermission.<init>(BasicPermission.java:131) at java.base/java.net.NetPermission.<init>(NetPermission.java:205) at com.NetPermission.NetPermission1.main(NetPermission1.java:9)
Approach c: IllegalArgumentException
Java
package com.NetPermission;import java.net.NetPermission;public class NetPermission1 {public static void main(String[] args) {String name = "";NetPermission netPermission = new NetPermission(name);System.out.println(netPermission);}}
Output:
Exception in thread "main" java.lang.IllegalArgumentException: name can't be empty at java.base/java.security.BasicPermission.init(BasicPermission.java:94) at java.base/java.security.BasicPermission.<init>(BasicPermission.java:131) at java.base/java.net.NetPermission.<init>(NetPermission.java:205) at com.NetPermission.NetPermission1.main(NetPermission1.java:9)
2. NetPermission.NetPermission(String name, String actions)
java.net.NetPermission.NetPermission(String name, String actions)
This method takes two arguments. This method creates a new NetPermission object with the specified name. The name is the symbolic name of the NetPermission, and the actions String is currently unused and should be null.
Parameters: Two parameters are required for this method.
name: the name of the NetPermission.
actions: should be null.
Throws:
1. NullPointerException - if the name is null.
2. IllegalArgumentException - if the name is empty.
Approach a: When no exception
Java
package com.NetPermission;import java.net.NetPermission;public class NetPermission2 {public static void main(String[] args) {String name = "hello";String action = "read";NetPermission netPermission = new NetPermission(name, action);System.out.println(netPermission);}}
Output:
("java.net.NetPermission" "hello")
Approach b: NullPointerException
Java
package com.NetPermission;import java.net.NetPermission;public class NetPermission2 {public static void main(String[] args) {String name = null;String action = "read";NetPermission netPermission = new NetPermission(name, action);System.out.println(netPermission);}}
Output:
Exception in thread "main" java.lang.NullPointerException: name can't be null at java.base/java.security.BasicPermission.init(BasicPermission.java:89) at java.base/java.security.BasicPermission.<init>(BasicPermission.java:148) at java.base/java.net.NetPermission.<init>(NetPermission.java:222) at com.NetPermission.NetPermission2.main(NetPermission2.java:10)
Approach c: IllegalArgumentException
Java
package com.NetPermission;import java.net.NetPermission;public class NetPermission2 {public static void main(String[] args) {String name = "";String action = "read";NetPermission netPermission = new NetPermission(name, action);System.out.println(netPermission);}}
Output:
Exception in thread "main" java.lang.IllegalArgumentException: name can't be empty at java.base/java.security.BasicPermission.init(BasicPermission.java:94) at java.base/java.security.BasicPermission.<init>(BasicPermission.java:148) at java.base/java.net.NetPermission.<init>(NetPermission.java:222) at com.NetPermission.NetPermission2.main(NetPermission2.java:10)
No comments:
Post a Comment