<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: "Calibri Light", "Helvetica Light", sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
Hi Rob,</div>
<div style="font-family: "Calibri Light", "Helvetica Light", sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
Can you share an actual piece of code that would benefit from such a lambda migration?</div>
<div style="font-family: "Calibri Light", "Helvetica Light", sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
I have checked the library code of java.base module; the try-with-resources and finally are mostly used for I/O and security cleanups, none of which would significantly benefit from this improvement.</div>
<div style="font-family: "Calibri Light", "Helvetica Light", sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
In particular, I noted this form would only be more useful than finally with these preconditions:</div>
<ol style="margin-top: 0px; margin-bottom: 0px; list-style-type: decimal;" data-editing-info="{"applyListStyleFromLevel":false,"orderedStyleType":1}" start="1">
<li style="font-family: "Calibri Light", "Helvetica Light", sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<div role="presentation" class="elementToProof">The cleanup action must be throwing some exception</div>
</li><li style="font-family: "Calibri Light", "Helvetica Light", sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<div role="presentation" class="elementToProof">The cleanup action doesn't make use of local variables</div>
</li></ol>
<div style="font-family: "Calibri Light", "Helvetica Light", sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
Which IMO makes this usage much more niche.</div>
<div style="font-family: "Calibri Light", "Helvetica Light", sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
Finally, for your proposed solution, I think we can accomplish that with a subinterface of AutoCloseable that has this type parameter - this does not require existing implementors to update, yet offers the same benefits.</div>
<div style="font-family: "Calibri Light", "Helvetica Light", sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<br>
</div>
<div style="font-family: "Calibri Light", "Helvetica Light", sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
Regards,</div>
<div style="font-family: "Calibri Light", "Helvetica Light", sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
Chen Liang</div>
<div id="appendonsend"></div>
<div><br>
<div style="font-family: Calibri; text-align: left; color: rgb(0, 0, 0); margin-left: 5pt; font-size: 10pt;">
Confidential – Oracle Internal</div>
</div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> core-libs-dev <core-libs-dev-retn@openjdk.org> on behalf of Rob Spoor <openjdk@icemanx.nl><br>
<b>Sent:</b> Tuesday, October 7, 2025 2:43 PM<br>
<b>To:</b> core-libs-dev@openjdk.java.net <core-libs-dev@openjdk.java.net><br>
<b>Subject:</b> RFE: make AutoCloseable generic in its exception type</font>
<div> </div>
</div>
<div class="BodyFragment"><font size="2"><span style="font-size:11pt;">
<div class="PlainText">Hi all,<br>
<br>
AutoCloseable, as added in Java 7 and unchanged ever since, declares to <br>
throw Exception from its close() method. That means that anytime someone <br>
needs to have an AutoCloseable that throws a different exception, they <br>
must create an interface that extends AutoCloseable and override close() <br>
only to throw a different (or no) exception. That can become quite <br>
annoying if you have to do it often. But without such interfaces, we <br>
cannot use lambdas or method references in try-with-resources without <br>
having to deal with Exception:<br>
<br>
     try (AutoCloseable _ = this::cleanup) {<br>
     } // need to catch or re-throw Exception<br>
<br>
<br>
I think that making AutoCloseable generic in its exception type is a <br>
change that's quite simple, yet makes life easier for developers. The <br>
interface would become this:<br>
<br>
     public interface AutoCloseable<X extends Exception> {<br>
         /**<br>
          * <existing javadoc minus throws<br>
          *<br>
          * @throws X if this resource cannot be closed<br>
          */<br>
         void close() throws X;<br>
     }<br>
<br>
In its raw form, the interface is exactly what we already have: close() <br>
throws Exception. Therefore, any use of AutoCloseable that doesn't fill <br>
in the generic type (including extending interfaces) will work as they <br>
do now. They can still override close() and change the thrown exception <br>
type. Interfaces that extend AutoCloseable can fill in the generic type, <br>
and if the close() method doesn't add additional documentation it can be <br>
removed completely. Interfaces like Closeable will only have to change <br>
the extends clause:<br>
<br>
     public interface Closeable extends AutoCloseable<IOException> {<br>
         // keep close for its documentation<br>
     }<br>
<br>
That doesn't add much value yet, but we can now use it in <br>
try-with-resources without having to deal with Exception:<br>
<br>
     try (AutoCloseable<RuntimeException> c = this::cleanup) {<br>
     } // no need to catch or re-throw RuntimeException<br>
<br>
<br>
There is one other possible change that can make it even easier, and <br>
that is adding a factory method like Gatherer.Integrator.of:<br>
<br>
     public interface AutoCloseable<X extends Exception> {<br>
         // documentation omitted<br>
         void close() throws X;<br>
<br>
         // documentation omitted<br>
         static <X extends Exception> AutoCloseable<X> <br>
of(AutoCloseable<X> closeable) {<br>
             return closeable;<br>
         }<br>
     }<br>
<br>
The try-with-resources can now become even simpler:<br>
<br>
    try (var _ = AutoCloseable.of(this::cleanup)) {<br>
    }<br>
<br>
<br>
There is one possible issue with this "of" method, and that is possible <br>
incompatibilities with existing implementing classes or extending <br>
interfaces that already have an "of(AutoCloseable)" method with a <br>
different return type, or that are not static. I think the chances of <br>
that are not great, but it still is something to consider.<br>
<br>
<br>
Any thoughts?<br>
<br>
</div>
</span></font></div>
</body>
</html>