RFR: 8066869: Add Closeable::closeUnchecked that is the equivalent of close but throws UncheckedIOException

Brian Burkhalter bpb at openjdk.org
Thu Jul 6 20:57:54 UTC 2023


On Thu, 6 Jul 2023 20:42:34 GMT, Roger Riggs <rriggs at openjdk.org> wrote:

> Can you add an example showing the recommended pattern of use?

One pattern is for example when reading from a stream and closing it is highly unlikely to generate an exception, then instead of the try block in

InputStream in = ...;
while(someCondition) {
    int b = in.read();
    // process b
}
try {
    in.close();
} catch (IOException ignored) {
}

one could simply write

in.closeUnchecked();

and avoid the boilerplate imposed by the checked exception. This sort of code can be found in many places in the tests in `<OpenJDK>/test/jdk`.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/14789#discussion_r1254910730


More information about the core-libs-dev mailing list