public final class ThrowingEx extends Object
Basic functional interfaces which throw exception, along with static helper methods for calling them.
Contains most of the functionality of Durian’s Throwing and Errors classes, but stripped down and renamed to avoid any confusion.
| Modifier and Type | Class and Description |
|---|---|
static interface |
ThrowingEx.BiFunction<T1,T2,R>
A bi-function that can throw any exception.
|
static interface |
ThrowingEx.Function<T,R>
A function that can throw any exception.
|
static interface |
ThrowingEx.Runnable
A runnable that can throw any exception.
|
static interface |
ThrowingEx.Supplier<T>
A supplier that can throw any exception.
|
static class |
ThrowingEx.WrappedAsRuntimeException
A RuntimeException specifically for the purpose of wrapping non-runtime Exceptions as RuntimeExceptions.
|
| Modifier and Type | Method and Description |
|---|---|
static RuntimeException |
asRuntime(Exception e)
Casts or wraps the given exception to be a RuntimeException.
|
static <T> T |
get(ThrowingEx.Supplier<T> supplier)
Gets the given value, rethrowing any exceptions as runtime exceptions.
|
static void |
run(ThrowingEx.Runnable runnable)
Runs the given runnable, rethrowing any exceptions as runtime exceptions.
|
static RuntimeException |
unwrapCause(Throwable e)
Utility method for rethrowing an exception’s cause with as few wrappers as possible.
|
static <T,R> Function<T,R> |
wrap(ThrowingEx.Function<T,R> function)
Wraps the given
ThrowingEx.Function as a standard Function, rethrowing any exceptions as runtime exceptions. |
public static void run(ThrowingEx.Runnable runnable)
Runs the given runnable, rethrowing any exceptions as runtime exceptions.
public static <T> T get(ThrowingEx.Supplier<T> supplier)
Gets the given value, rethrowing any exceptions as runtime exceptions.
public static <T,R> Function<T,R> wrap(ThrowingEx.Function<T,R> function)
Wraps the given ThrowingEx.Function as a standard Function, rethrowing any exceptions as runtime exceptions.
public static RuntimeException asRuntime(Exception e)
Casts or wraps the given exception to be a RuntimeException.
If the input exception is a RuntimeException, it is simply cast and returned. Otherwise, it wrapped in a ThrowingEx.WrappedAsRuntimeException and returned.
public static RuntimeException unwrapCause(Throwable e)
Utility method for rethrowing an exception’s cause with as few wrappers as possible.
try {
doSomething();
} catch (Throwable e) {
throw unwrapCause(e);
}