JDK-8242172 : Path.toAbsolutePath() and Path.toUri() should not throw IOError
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.nio
  • Affected Version: 14
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • Submitted: 2020-04-03
  • Updated: 2020-04-06
  • Resolved: 2020-04-06
Related Reports
Relates :  
Description
A DESCRIPTION OF THE PROBLEM :
Path.toAbsolutePath() and Path.toUri() can throw IOErrors.
IOError extends Error and therefore the following from the Error javadoc must hold true:
>An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch.

However, toAbsolutePath() and toUri() throw it for mundane issues like non existing paths, which violates the Error javadoc. And even the JDK suffers from this because in sun.nio.fs.WindowsLinkSupport.getRealPath it has to catch the IOError.

Therefore please do not allow toAbsolutePath() and toUri() to throw IOErrors. Instead choose an existing RuntimeException subclass or create a new one.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the code provided below. It uses non-existent / invalid paths.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
A RuntimeException should be thrown.
ACTUAL -
An IOError error is thrown for something as mundane as an invalid path.

---------- BEGIN SOURCE ----------
try {
    // On Windows; assumes drive Z does not exist
    Path.of("Z:test").toAbsolutePath();
} catch (IOError ioError) {
    // IOError is thrown for a non-existing path
    ioError.printStackTrace();
}
try {
    // Path must start with /modules, see JDK-8224946
    FileSystems.getFileSystem(URI.create("jrt:/")).getPath("/").toUri();
} catch (IOError ioError) {
    // IOError is thrown for malformed path
    ioError.printStackTrace();
}
---------- END SOURCE ----------


Comments
These methods are specifies to throw IOError. They predate UncheckedIOException. Changing them now would be an incompatible change.
06-04-2020

The observation on Windows 10: JDK 14: Fail ILW=MML=P4
06-04-2020