garydgregory commented on code in PR #818:
URL: https://github.com/apache/commons-io/pull/818#discussion_r2614018376


##########
src/main/java/org/apache/commons/io/IOUtils.java:
##########
@@ -824,7 +824,40 @@ private static void closeQ(final Closeable closeable) {
      * @see Throwable#addSuppressed(Throwable)
      */
     public static void closeQuietly(final Closeable closeable) {
-        closeQuietly(closeable, null);
+        closeQuietly(closeable, (Consumer<Exception>) null);
+    }
+
+    /**
+     * Closes a {@link Closeable} unconditionally and adds any exception 
thrown by the {@code close()} to the given Throwable.
+     *
+     * <p>
+     * For example:
+     * </p>
+     *
+     * <pre>
+     * Closeable closeable = ...;
+     * try {
+     *     // process closeable
+     *     closeable.close();
+     * } catch (Exception e) {
+     *     // error handling
+     *     throw IOUtils.closeQuietly(closeable, e);
+     * }
+     * </pre>
+     * <p>
+     * Also consider using a try-with-resources statement where appropriate.
+     * </p>
+     *
+     * @param <T> The Throwable type.
+     * @param closeable The object to close, may be null or already closed.
+     * @param throwable Add the exception throw by the closeable to the given 
Throwable.
+     * @return The given Throwable.
+     * @since 2.22.0
+     * @see Throwable#addSuppressed(Throwable)
+     */
+    public static <T extends Throwable> T closeQuietly(final Closeable 
closeable, final T throwable) {

Review Comment:
   There is no type casting needed in the call sites in:
   
   - org.apache.commons.io.FileUtils.lineIterator(File, String)
   - org.apache.commons.io.LineIterator.hasNext()
   - org.apache.commons.io.output.FileWriterWithEncoding.initWriter(File, 
Object, boolean)
   - 
org.apache.commons.io.IOUtilsTest.testCloseQuietly_CloseableIOExceptionAddSuppressed()
   
   I would only see a typecast needed for a null literal argument, and making 
the method weird compared to the others  for this one use case doesn't seem 
worth it to me.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to