On Sat, 2 Mar 2024 00:44:02 GMT, Chris Plummer <cjplum...@openjdk.org> wrote:

>> test/hotspot/jtreg/serviceability/dcmd/compiler/PerfMapTest.java line 103:
>> 
>>> 101:         } while(Files.exists(path));
>>> 102:         run(new JMXExecutor(), "Compiler.perfmap " + path.toString(), 
>>> path);
>>> 103:     }
>> 
>> Q1: It is not clear why the file is not deleted in this case. Do I miss 
>> anything?
>> Q2: Can we use `Files.deleteIfExists(path)` in the `finally block` of the 
>> `run()` method instead?
>> 
>> 
>>     public void run(CommandExecutor executor, String cmd, Path path) {
>>        try {
>>            <body of run() method>
>>        } finally {
>>            Files.deleteIfExists(path);
>>        }
>>     }
>> 
>> One more approach is to wrap `run()` call into the `runHelper()` method:
>> 
>>     public void runHelper(CommandExecutor executor, String cmd, Path path) {
>>        try {
>>            run(executor, cmd, path);
>>        } finally {
>>            Files.deleteIfExists(path);
>>        }
>>     }
>
>>  Q2: Can we use Files.deleteIfExists(path) in the finally block of the run() 
>> method instead?
> 
> I don't think that changes anything. If run() throws an exception and then 
> Files.deleteIfExists() throws an exception from the finally block, the 
> original exception is lost.
> 
>> One more approach is to wrap run() call into the runHelper() method:
> 
> Same issue with potentially losing the original exception.

Two variants:

    public void runHelper(CommandExecutor executor, String cmd, Path path) {
       try {
           run(executor, cmd, path);
       } catch (Throwable t) {
            t.printStackTrace(System.out);
            throw t;
       } finally {
           Files.deleteIfExists(path);
       }
    }

or

    public void runHelper(CommandExecutor executor, String cmd, Path path) {
       try {
           run(executor, cmd, path);
       } finally {
           try {
              Files.deleteIfExists(path);
           } catch (Throwable t) {
              <print message about exception thrown from deleteIfExists>
              t.printStackTrace(System.out);
           }
       }
    }

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/17992#discussion_r1509803616

Reply via email to