@@ -780,7 +780,7 @@ public static void close(final URLConnection conn) {
780780 * @param closeable the object to close, may be null.
781781 */
782782 private static void closeQ (final Closeable closeable ) {
783- closeQuietly (closeable , null );
783+ closeQuietly (closeable , ( Consumer < Exception >) null );
784784 }
785785
786786 /**
@@ -824,7 +824,40 @@ private static void closeQ(final Closeable closeable) {
824824 * @see Throwable#addSuppressed(Throwable)
825825 */
826826 public static void closeQuietly (final Closeable closeable ) {
827- closeQuietly (closeable , null );
827+ closeQuietly (closeable , (Consumer <Exception >) null );
828+ }
829+
830+ /**
831+ * Closes a {@link Closeable} unconditionally and adds any exception thrown by the {@code close()} to the given Throwable.
832+ *
833+ * <p>
834+ * For example:
835+ * </p>
836+ *
837+ * <pre>
838+ * Closeable closeable = ...;
839+ * try {
840+ * // process closeable
841+ * closeable.close();
842+ * } catch (Exception e) {
843+ * // error handling
844+ * throw IOUtils.closeQuietly(closeable, e);
845+ * }
846+ * </pre>
847+ * <p>
848+ * Also consider using a try-with-resources statement where appropriate.
849+ * </p>
850+ *
851+ * @param <T> The Throwable type.
852+ * @param closeable The object to close, may be null or already closed.
853+ * @param throwable Add the exception throw by the closeable to the given Throwable.
854+ * @return The given Throwable.
855+ * @since 2.22.0
856+ * @see Throwable#addSuppressed(Throwable)
857+ */
858+ public static <T extends Throwable > T closeQuietly (final Closeable closeable , final T throwable ) {
859+ closeQuietly (closeable , (Consumer <Exception >) throwable ::addSuppressed );
860+ return throwable ;
828861 }
829862
830863 /**
0 commit comments