public final class Unchecked extends Object
Two wrap() methods are provided that can wrap an arbitrary piece of logic
and convert checked exceptions to unchecked.
A number of other methods are provided that allow a lambda block to be decorated
to avoid handling checked exceptions.
For example, the method File.getCanonicalFile() throws an IOException
which can be handled as follows:
stream.map(Unchecked.function(file -> file.getCanonicalFile())
Each method accepts a functional interface that is defined to throw Throwable.
Catching Throwable means that any method can be wrapped.
Any InvocationTargetException is extracted and processed recursively.
Any IOException is converted to an UncheckedIOException.
Any ReflectiveOperationException is converted to an UncheckedReflectiveOperationException.
Any Error or RuntimeException is re-thrown without alteration.
Any other exception is wrapped in a RuntimeException.
| Modifier and Type | Method and Description |
|---|---|
static <T,U> BiConsumer<T,U> |
biConsumer(CheckedBiConsumer<T,U> consumer)
Converts checked exceptions to unchecked based on the
BiConsumer interface. |
static <T,U,R> BiFunction<T,U,R> |
biFunction(CheckedBiFunction<T,U,R> function)
Converts checked exceptions to unchecked based on the
BiFunction interface. |
static <T> BinaryOperator<T> |
binaryOperator(CheckedBinaryOperator<T> function)
Converts checked exceptions to unchecked based on the
BinaryOperator interface. |
static <T,U> BiPredicate<T,U> |
biPredicate(CheckedBiPredicate<T,U> predicate)
Converts checked exceptions to unchecked based on the
BiPredicate interface. |
static <T> Consumer<T> |
consumer(CheckedConsumer<T> consumer)
Converts checked exceptions to unchecked based on the
Consumer interface. |
static <T,R> Function<T,R> |
function(CheckedFunction<T,R> function)
Converts checked exceptions to unchecked based on the
Function interface. |
static <T> Predicate<T> |
predicate(CheckedPredicate<T> predicate)
Converts checked exceptions to unchecked based on the
Predicate interface. |
static RuntimeException |
propagate(Throwable throwable)
Propagates
throwable as-is if possible, or by wrapping in a RuntimeException if not. |
static Runnable |
runnable(CheckedRunnable runnable)
Converts checked exceptions to unchecked based on the
Runnable interface. |
static <R> Supplier<R> |
supplier(CheckedSupplier<R> supplier)
Converts checked exceptions to unchecked based on the
Supplier interface. |
static <T> UnaryOperator<T> |
unaryOperator(CheckedUnaryOperator<T> function)
Converts checked exceptions to unchecked based on the
UnaryOperator interface. |
static void |
wrap(CheckedRunnable block)
Wraps a block of code, converting checked exceptions to unchecked.
|
static <T> T |
wrap(CheckedSupplier<T> block)
Wraps a block of code, converting checked exceptions to unchecked.
|
public static void wrap(CheckedRunnable block)
Unchecked.wrap(() -> {
// any code that throws a checked exception
}
If a checked exception is thrown it is converted to an UncheckedIOException
or RuntimeException as appropriate.
block - the code block to wrapUncheckedIOException - if an IO exception occursRuntimeException - if an exception occurspublic static <T> T wrap(CheckedSupplier<T> block)
Unchecked.wrap(() -> {
// any code that throws a checked exception
}
If a checked exception is thrown it is converted to an UncheckedIOException
or RuntimeException as appropriate.
T - the type of the resultblock - the code block to wrapUncheckedIOException - if an IO exception occursRuntimeException - if an exception occurspublic static Runnable runnable(CheckedRunnable runnable)
Runnable interface.
This wraps the specified runnable returning an instance that handles checked exceptions.
If a checked exception is thrown it is converted to an UncheckedIOException
or RuntimeException as appropriate.
runnable - the runnable to be decoratedpublic static <T,R> Function<T,R> function(CheckedFunction<T,R> function)
Function interface.
This wraps the specified function returning an instance that handles checked exceptions.
If a checked exception is thrown it is converted to an UncheckedIOException
or RuntimeException as appropriate.
T - the input type of the functionR - the return type of the functionfunction - the function to be decoratedpublic static <T,U,R> BiFunction<T,U,R> biFunction(CheckedBiFunction<T,U,R> function)
BiFunction interface.
This wraps the specified function returning an instance that handles checked exceptions.
If a checked exception is thrown it is converted to an UncheckedIOException
or RuntimeException as appropriate.
T - the first input type of the functionU - the second input type of the functionR - the return type of the functionfunction - the function to be decoratedpublic static <T> UnaryOperator<T> unaryOperator(CheckedUnaryOperator<T> function)
UnaryOperator interface.
This wraps the specified operator returning an instance that handles checked exceptions.
If a checked exception is thrown it is converted to an UncheckedIOException
or RuntimeException as appropriate.
T - the type of the operatorfunction - the function to be decoratedpublic static <T> BinaryOperator<T> binaryOperator(CheckedBinaryOperator<T> function)
BinaryOperator interface.
This wraps the specified operator returning an instance that handles checked exceptions.
If a checked exception is thrown it is converted to an UncheckedIOException
or RuntimeException as appropriate.
T - the type of the operatorfunction - the function to be decoratedpublic static <T> Predicate<T> predicate(CheckedPredicate<T> predicate)
Predicate interface.
This wraps the specified predicate returning an instance that handles checked exceptions.
If a checked exception is thrown it is converted to an UncheckedIOException
or RuntimeException as appropriate.
T - the type of the predicatepredicate - the predicate to be decoratedpublic static <T,U> BiPredicate<T,U> biPredicate(CheckedBiPredicate<T,U> predicate)
BiPredicate interface.
This wraps the specified predicate returning an instance that handles checked exceptions.
If a checked exception is thrown it is converted to an UncheckedIOException
or RuntimeException as appropriate.
T - the first type of the predicateU - the second type of the predicatepredicate - the predicate to be decoratedpublic static <T> Consumer<T> consumer(CheckedConsumer<T> consumer)
Consumer interface.
This wraps the specified consumer returning an instance that handles checked exceptions.
If a checked exception is thrown it is converted to an UncheckedIOException
or RuntimeException as appropriate.
T - the type of the consumerconsumer - the consumer to be decoratedpublic static <T,U> BiConsumer<T,U> biConsumer(CheckedBiConsumer<T,U> consumer)
BiConsumer interface.
This wraps the specified consumer returning an instance that handles checked exceptions.
If a checked exception is thrown it is converted to an UncheckedIOException
or RuntimeException as appropriate.
T - the first type of the consumerU - the second type of the consumerconsumer - the consumer to be decoratedpublic static <R> Supplier<R> supplier(CheckedSupplier<R> supplier)
Supplier interface.
This wraps the specified supplier returning an instance that handles checked exceptions.
If a checked exception is thrown it is converted to an UncheckedIOException
or RuntimeException as appropriate.
R - the result type of the suppliersupplier - the supplier to be decoratedpublic static RuntimeException propagate(Throwable throwable)
throwable as-is if possible, or by wrapping in a RuntimeException if not.
throwable is an InvocationTargetException the cause is extracted and processed recursively.throwable is an Error or RuntimeException it is propagated as-is.throwable is an IOException it is wrapped in UncheckedIOException and thrown.throwable is an ReflectiveOperationException it is wrapped in
UncheckedReflectiveOperationException and thrown.throwable is wrapped in a RuntimeException and thrown.
T foo() {
try {
return methodWithCheckedException();
} catch (Exception e) {
throw Unchecked.propagate(e);
}
}
throwable - the Throwable to propagateCopyright 2009-Present by OpenGamma Inc. and individual contributors
Apache v2 licensed
Additional documentation can be found at strata.opengamma.io.