The class defines a set of exceptions that are considered to be safe to be caught, which means that the operation will not abort if an exception of the following types are caught:
- StackOverflowError: The execution should be able to recover from this.
- OutOfMemoryError: The execution should be able to recover from this.
- LinkageError: Happens when classes are no longer compatible with each other. This usually means that the runtime classpath is different from the compile classpath. This is usually non recoverable, or hard to do, but is safe to catch, as this doesn't hinder the further operations on different objects.
- ServiceConfigurationError: When some service configuration is invalid. This can be caught, as this is a recoverable error that is more like a RuntimeException.
- AssertionError: In case some assertion failed. An error of this kind is recoverable, as this doesn't signal a VM error, but only some runtime validation failed in some implementation.
- Exception: Generic exception, can be caught.
safe
, and is referred to such
in the documentation of utility functions. As a general rule of thumb, if an operation (e.g. closing) is to be
performed on multiple objects, and a safe exception is caught, the operation will continue to execute the action on
the remaining elements, and throw the exception after the action was performed on all of the objects.
The term safe errors
contain only the Error types from the safe exceptions.
Many of the methods in this class work with AutoCloseable instances, but declare only IOException as its thrown exception types. When an AutoCloseable throws an Exception declared by its AutoCloseable.close() method, it will be wrapped into an IOException, and rethrown as that. As in most cases users work with Closeable types, which throw IOException, this is an acceptable compromise to support AutoCloseable types as well, but have them work like Closeables.
If the above happens for multiple AutoCloseables, only the first caught exception will be wrapped into an IOException, the laters will be added as suppressed exceptions to the first caught exception.
Methods that start with close
will call AutoCloseable.close() on the argument object(s). The
closing will occur for all argument objects, and any exception caught will be handled according to each method
documentation. Using this methods can be useful when multiple closeables need to be closed without having a nested
try-finally block in the caller method.
Example:
Instead of closing multiple closeables like this:
try { closeable1.close(); } finally { try { closeable2.close(); } finally { try { closeable3.close(); } finally { closeable4.close(); } } }Use:
IOUtils.close(closeable1, closeable2, closeable3, closeable4);The latter approach also have the advantage that all the exceptions will be reported as suppressed, or cause exceptions, instead of just the last in the first approach.
The closing methods in this class will silently ignore if the argument collection is null
, or any of the
closeable object is null
.
public static IOException | addExc( Aggregates two exceptions with an IOException base. |
public static < | addExc( Aggregates two exceptions of the same type. |
public static < | Aggregates two exceptions of the different type using the specified creator function. |
public static void | close( Calls AutoCloseable.close() for every object in the argument iterable, and throws any exceptions at the
end. |
public static void | close( Calls AutoCloseable.close() for every object in the argument iterable, and throws any exceptions at the
end. |
public static void | close( Calls AutoCloseable.close() for every object in the argument iterable, and throws any exceptions at the
end. |
public static IOException | closeExc( Calls AutoCloseable.close() for every object in the argument iterable, and aggregates any thrown
exception with the previous argument. |
public static IOException | closeExc( Calls AutoCloseable.close() for every object in the argument iterable, and aggregates any thrown
exception with the previous argument. |
public static IOException | closeExc( Calls AutoCloseable.close() for every object in the argument iterable, and aggregates any thrown
exception with the previous argument. |
public static IOException | closeExc( Calls AutoCloseable.close() for every object in the argument iterable, and returns the thrown exception
if any. |
public static IOException | closeExc( Calls AutoCloseable.close() for every object in the argument iterable, and returns the thrown exception
if any. |
public static IOException | closeExc( Calls AutoCloseable.close() for every object in the argument iterable, and returns the thrown exception
if any. |
public static void | closeIgnore( Calls AutoCloseable.close() for every object in the argument iterable, and ignores the possibly thrown
exceptions. |
public static void | closeIgnore( Calls AutoCloseable.close() for every object in the argument iterable, and ignores the possibly thrown
exceptions. |
public static void | closeIgnore( Calls AutoCloseable.close() for every object in the argument iterable, and ignores the possibly thrown
exceptions. |
public static void | closePrint( Calls AutoCloseable.close() for every object in the argument iterable, and prints the stack trace of the
possibly thrown exceptions. |
public static void | closePrint( Calls AutoCloseable.close() for every object in the argument iterable, and prints the stack trace of the
possibly thrown exceptions. |
public static void | closePrint( Calls AutoCloseable.close() for every object in the argument iterable, and prints the stack trace of the
possibly thrown exceptions. |
public static < | collectExc( Collects the argument exception into a list. |
public static < | foreach( Executes the given I/O action for every object in the argument iterable. |
public static void | Acquires the lock in interruptable mode, and throws InterruptedIOException if the current thread is
interrupted. |
public static void | Acquires the lock in interruptable mode, and throws InterruptedIOException if the current thread is
interrupted. |
public static void | Prints the stacktrace of the argument exception if it is non- null . |
public static < | throwExc( Throws the argument exception if it is non- null . |
public static boolean | Attempts to acquire the lock in interruptable mode, and throws InterruptedIOException if the current
thread is interrupted. |
public static boolean | Attempts to acquire the lock in interruptable mode, and throws InterruptedIOException if the current
thread is interrupted. |
This method takes two exception arguments (only one of them can be non-null
), and returns an
aggregated exception based on them
If the next exception is null
, the argument prev
is returned.
If the prev
argument is null
, the next will be returned, possibly wrapping it in an
IOException. If the next
is already an instance if IOException, it will be plainly
returned.
If both are non-null
, then the next
will be added to prev
as suppressed
exception, and prev
is returned.
null
if there was none.null
if there was none.
This method works the same ways as addExc(next
exception into an IOException.
null
if there was none.null
if there was none.
This method works the same ways as addExc(next
exception into the type of the previous exception.
null
if there was none.null
if there was none.null
.Any thrown safe errors defined by this class do not abort the operation, but they are rethrown at the end.
Any thrown safe errors defined by this class do not abort the operation, but they are rethrown at the end.
Any thrown safe errors defined by this class do not abort the operation, but they are rethrown at the end.
Any thrown safe errors defined by this class do not abort the operation, but they are collected.
If the previous argument serves as a base when collecting thrown exceptions. If there is a previous exception, any thrown exception will be added as suppressed exception to it, and that will be returned. If there is no previous, the caught exceptions is returned.
It is strongly recommended that callers handle any returned exception somehow instead of ignoring it.
This method can be used in a chained way:
IOException exc = null; exc = IOUtils.closeExc(exc, closeable1); exc = IOUtils.closeExc(exc, closeable2, closeable3); //handle the exception somehow IOUtils.throwExc(exc);
null
if there was none.null
, or the caught exception during closing, or
null
if there was none.Any thrown safe errors defined by this class do not abort the operation, but they are collected.
If the previous argument serves as a base when collecting thrown exceptions. If there is a previous exception, any thrown exception will be added as suppressed exception to it, and that will be returned. If there is no previous, the caught exceptions is returned.
It is strongly recommended that callers handle any returned exception somehow instead of ignoring it.
This method can be used in a chained way:
IOException exc = null; exc = IOUtils.closeExc(exc, closeable1); exc = IOUtils.closeExc(exc, closeable2, closeable3); //handle the exception somehow IOUtils.throwExc(exc);
null
if there was none.null
, or the caught exception during closing, or
null
if there was none.Any thrown safe errors defined by this class do not abort the operation, but they are collected.
If the previous argument serves as a base when collecting thrown exceptions. If there is a previous exception, any thrown exception will be added as suppressed exception to it, and that will be returned. If there is no previous, the caught exceptions is returned.
It is strongly recommended that callers handle any returned exception somehow instead of ignoring it.
This method can be used in a chained way:
IOException exc = null; exc = IOUtils.closeExc(exc, closeable1); exc = IOUtils.closeExc(exc, closeable2, closeable3); //handle the exception somehow IOUtils.throwExc(exc);
null
if there was none.null
, or the caught exception during closing, or
null
if there was none.Any thrown safe errors defined by this class do not abort the operation, but they are collected.
It is strongly recommended that callers handle any returned exception somehow instead of ignoring it.
null
if there was none.Any thrown safe errors defined by this class do not abort the operation, but they are collected.
It is strongly recommended that callers handle any returned exception somehow instead of ignoring it.
null
if there was none.Any thrown safe errors defined by this class do not abort the operation, but they are collected.
It is strongly recommended that callers handle any returned exception somehow instead of ignoring it.
null
if there was none.Any thrown safe errors defined by this class do not abort the operation, but they are rethrown at the end.
Any thrown safe errors defined by this class do not abort the operation, but they are rethrown at the end.
Any thrown safe errors defined by this class do not abort the operation, but they are rethrown at the end.
Any thrown safe errors defined by this class do not abort the operation, but they are rethrown at the end.
Any Exceptions thrown from closing methods will be printed using Throwable.printStackTrace().
Any thrown safe errors defined by this class do not abort the operation, but they are rethrown at the end.
Any Exceptions thrown from closing methods will be printed using Throwable.printStackTrace().
Any thrown safe errors defined by this class do not abort the operation, but they are rethrown at the end.
Any Exceptions thrown from closing methods will be printed using Throwable.printStackTrace().
This method will add the argument exception to the specified List instance. If the given list is
null
, a new one will be instantiated, and returned. If non-null
, the exception is added
to it, and the list itself is returned.
If the exception is null
, the argument list is directly returned.
This method can be used in a chain to lazily instantiate an exception container when any exceptions is thrown. Example:
List<Exception> excs = null; while (condition) { try { //some work } catch (Exception e) { excs = IOUtils.collectExc(excs, e); } } //handle the collected exceptions if any.
null
, if none.The action will called in order for every object in the iterable. If an action throws an exception, it will be rethrown from this method after the remaining actions for the objects are called. Any thrown safe exception defined by this class do not abort the operation.
null
.If the locking is interrupted, the current thread interrupt status will be set.
null
.If the locking is interrupted, the current thread interrupt status will be set.
null
)null
.null
.null
.null
.If the locking is interrupted, the current thread interrupt status will be set.
time
argument.true
if the lock was acquired and false
if the waiting time elapsed before the
lock was acquired.null
.If the locking is interrupted, the current thread interrupt status will be set.
time
argument.null
)true
if the lock was acquired and false
if the waiting time elapsed before the
lock was acquired.null
.