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

Remi Forax forax at univ-mlv.fr
Thu Jul 6 21:43:59 UTC 2023


I'm not a big fan of hiding exceptions, why not wrapping the IOException in an UncheckedIOException ?

So closeUnchecked() means close and throw an unchecked exception if an exception occurs.

regards,
Rémi

----- Original Message -----
> From: "Brian Burkhalter" <bpb at openjdk.org>
> To: "core-libs-dev" <core-libs-dev at openjdk.org>
> Sent: Thursday, July 6, 2023 11:57:54 PM
> Subject: Re: RFR: 8066869: Add Closeable::closeUnchecked that is the equivalent of close but throws UncheckedIOException

> 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