Package io.toro.martini.util
Class Throwables
- java.lang.Object
-
- io.toro.martini.util.Throwables
-
public final class Throwables extends Object
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
Throwables.ThrowingConsumer<T>
Taken from https://gist.github.com/myui/9722c1301434a3b69cf898ccd9090ff1.static interface
Throwables.ThrowingFunction<T,R>
static interface
Throwables.ThrowingSupplier<T>
Sneakily throw exception from a supplier
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <E extends Throwable,R>
RfunctionSneakyThrow(Throwable ex)
Allows transforming of checked exceptions to unchecked.static Throwable
getRootCause(Throwable throwable)
Returns the innermost cause ofthrowable
.static <T> Consumer<T>
rethrow(Throwables.ThrowingConsumer<T> consumer)
Provides a consumer that helps in rethrowing checked exception for lambdas.static <T> Supplier<T>
rethrow(Throwables.ThrowingSupplier<T> supplier)
Provides a supplier that helps in rethrowing checked exception for lambdas.static <T,R>
Function<T,R>rethrowFunction(Throwables.ThrowingFunction<T,R> function)
Provides a function that helps in rethrowing checked exception for lambdas.static <E extends Throwable>
voidsneakyThrow(Throwable ex)
Allows transforming of checked exceptions to unchecked.static void
throwIfUnchecked(Throwable throwable)
-
-
-
Method Detail
-
getRootCause
public static Throwable getRootCause(Throwable throwable)
Returns the innermost cause ofthrowable
. The first throwable in a chain provides context from when the error or exception was initially detected. Example usage:assertEquals("Unable to assign a customer id", Throwables.getRootCause(e).getMessage());
-
throwIfUnchecked
public static void throwIfUnchecked(Throwable throwable)
Throwsthrowable
if it is aRuntimeException
orError
. Example usage:for (Foo foo : foos) { try { foo.bar(); } catch (RuntimeException | Error t) { failure = t; } } if (failure != null) { throwIfUnchecked(failure); throw new AssertionError(failure); }
- Since:
- 20.0
-
rethrow
public static <T> Consumer<T> rethrow(Throwables.ThrowingConsumer<T> consumer)
Provides a consumer that helps in rethrowing checked exception for lambdas. Example:foo.bar( rethrow( param -> param.doSomething() ) )
If doSomething() throws a checked exception, compiler will allow it even if it's not wrapped in a try-catch block. The exception can be optionally caught using a try-catch block. Example:try { foo.bar( rethrow( param -> param.doSomething() ) ) } catch ( Exception e ) { // Exception can be caught here }
-
rethrow
public static <T> Supplier<T> rethrow(Throwables.ThrowingSupplier<T> supplier)
Provides a supplier that helps in rethrowing checked exception for lambdas.
-
sneakyThrow
public static <E extends Throwable> void sneakyThrow(Throwable ex) throws E extends Throwable
Allows transforming of checked exceptions to unchecked. The compiler sees the signature with the throws T inferred to a RuntimeException type, so it allows the unchecked exception to propagate.- Throws:
E extends Throwable
- See Also:
- Java Sneaky Throws, Throw Exceptions in Consumer in Java 8
-
rethrowFunction
public static <T,R> Function<T,R> rethrowFunction(Throwables.ThrowingFunction<T,R> function)
Provides a function that helps in rethrowing checked exception for lambdas. Example:foo.bar( rethrow( param -> param.returnSomething() ) )
If doSomething() throws a checked exception, compiler will allow it even if it's not wrapped in a try-catch block. The exception can be optionally caught using a try-catch block. Example:try { foo.bar( rethrow( param -> param.returnSomething() ) ) } catch ( Exception e ) { // Exception can be caught here }
-
functionSneakyThrow
public static <E extends Throwable,R> R functionSneakyThrow(Throwable ex) throws E extends Throwable
Allows transforming of checked exceptions to unchecked. The compiler sees the signature with the throws T inferred to a RuntimeException type, so it allows the unchecked exception to propagate.- Throws:
E extends Throwable
-
-