AutoCloseables¶
- public class org.apache.commons.lang3.AutoCloseables[source]¶
Static operations on
AutoCloseable.For
Closeable-specific methods, see Apache Commons IO’s IOUtils.- Since:
3.21.0
Methods¶
Closes the given |
|
Closes the given |
|
Closes an |
|
Closes the given |
|
Closes an iterable of |
|
Closes a |
- public static void close(AutoCloseable closeable) throws Exception[source]¶
Closes the given
AutoCloseableas a null-safe operation.- Parameters:
closeable – The resource to close, may be null.
- Throws:
java.lang.Exception – Thrown if an error occurs.
- public static void close(AutoCloseable closeable, FailableConsumer<Exception, Exception> consumer) throws Exception[source]¶
Closes the given
AutoCloseableas a null-safe operation.- Parameters:
closeable – The resource to close, may be null.
consumer – Consume the Exception thrown by
AutoCloseable#close().
- Throws:
java.lang.Exception – Thrown if the consumer throws an exception.
- public static void closeQuietly(AutoCloseable closeable)[source]¶
Closes an
AutoCloseable, never throwing anException.Equivalent to
AutoCloseable#close(), except any exceptions will be ignored.- Parameters:
closeable – The objects to close, may be null or already closed.
- See also:
- public static void closeQuietly(AutoCloseable closeable, Consumer<Exception> consumer)[source]¶
Closes the given
AutoCloseableas a null-safe operation while consuming Exception by the givenconsumer.- Parameters:
closeable – The resource to close, may be null.
consumer – Consumes the Exception thrown by
AutoCloseable#close().
- public static void closeQuietly(Iterable<AutoCloseable> closeables)[source]¶
Closes an iterable of
AutoCloseable, never throwing anException.Equivalent calling
AutoCloseable#close()on each element, except any exceptions will be ignored.- Parameters:
closeables – The objects to close, may be null or already closed.
- See also:
- public static <T extends Throwable> T closeQuietlySuppress(Closeable closeable, T throwable)[source]¶
Closes a
Closeableunconditionally and adds any exception thrown by theclose()to the given Throwable.For example:
AutoCloseable autoCloseable = ...; try { // process autoCloseable. } catch (Exception e) { // Handle exception. throw AutoCloseables.closeQuietlySuppress(autoCloseable, e); }
Also consider using a try-with-resources statement where appropriate.
- Type parameters:
<T> – The Throwable type.
- Parameters:
closeable – The object to close, may be null or already closed.
throwable – Add the exception throw by the closeable to the given Throwable.
- Returns:
The given Throwable.
- See also: