About Question enthuware.ocpjp.v11.2.3336 :
Posted: Sun Jan 31, 2021 8:38 pm
Are you sure that Option D is incorrect?
My .java.policy file content that causes the Exception. I commented out the line with the permission
My .java.policy file content that does not cause the Exception. I un-comment the permission line. Now the code returns true
The code won't throw an Exception if SecurityManager is not enabled, but the question did not let the reader know whether the SecurityManager was enabled or not.
It will print an exception stack trace the file referred to by p cannot be deleted due to lack of appropriate permissions.
I have this code and it does throw an ExceptionIt will not throw any exception in that case. It will just return false.
Code: Select all
public static void main(String[] args) throws IOException {
SecurityManager sm1 = new SecurityManager();
System.setSecurityManager(sm1);
Path p = Path.of("C:\\ioPractice\\deleteTest");
try{
var b = Files.deleteIfExists(p);
System.out.println(b);
}catch(Exception e){
e.printStackTrace();
}
}
Code: Select all
Exception in thread "main" java.security.AccessControlException: access denied ("java.io.FilePermission" "C:\ioPractice\deleteTest" "delete")
Code: Select all
grant {
//permission java.io.FilePermission "c:\\ioPractice\\deleteTest", "delete";
};
Code: Select all
grant {
permission java.io.FilePermission "c:\\ioPractice\\deleteTest", "delete";
};