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";
};