ThrowingSupplier

A Supplier that allows invocation of code that throws a checked exception.

API

ThrowingSupplier.java
interface ThrowingSupplier<T> {
  T getWithException()     (1)
  T get()     (2)
  T get(BiFunction<String, Exception, RuntimeException> exceptionWrapper)     (3)
  ThrowingSupplier<T> throwing(BiFunction<String, Exception, RuntimeException> exceptionWrapper)     (4)
  ThrowingSupplier<T> of(ThrowingSupplier<T> supplier)     (5)
  ThrowingSupplier<T> of(ThrowingSupplier<T> supplier, BiFunction<String, Exception, RuntimeException> exceptionWrapper)     (6)
}
1 getWithException()

Gets a result, possibly throwing a checked exception.

2 get()

Default Supplier#get() that wraps any thrown checked exceptions (by default in a RuntimeException ).

3 get(BiFunction)

Gets a result, wrapping any thrown checked exceptions using the given exceptionWrapper .

4 throwing(BiFunction)

Return a new ThrowingSupplier where the #get() method wraps any thrown checked exceptions using the given exceptionWrapper .

5 of(ThrowingSupplier)

Lambda friendly convenience method that can be used to create a ThrowingSupplier where the #get() method wraps any checked exception thrown by the supplied lambda expression or method reference.

6 of(ThrowingSupplier, BiFunction)

Lambda friendly convenience method that can be used to create ThrowingSupplier where the #get() method wraps any thrown checked exceptions using the given exceptionWrapper .

Members

getWithException()

Gets a result, possibly throwing a checked exception.

get()

Default Supplier#get() that wraps any thrown checked exceptions (by default in a RuntimeException ).

get(BiFunction)

Gets a result, wrapping any thrown checked exceptions using the given exceptionWrapper .

throwing(BiFunction)

Return a new ThrowingSupplier where the #get() method wraps any thrown checked exceptions using the given exceptionWrapper .

of(ThrowingSupplier)

Lambda friendly convenience method that can be used to create a ThrowingSupplier where the #get() method wraps any checked exception thrown by the supplied lambda expression or method reference.

This method can be especially useful when working with method references. It allows you to easily convert a method that throws a checked exception into an instance compatible with a regular Supplier .

For example:

optional.orElseGet(ThrowingSupplier.of(Example::methodThatCanThrowCheckedException));

of(ThrowingSupplier, BiFunction)

Lambda friendly convenience method that can be used to create ThrowingSupplier where the #get() method wraps any thrown checked exceptions using the given exceptionWrapper .

This method can be especially useful when working with method references. It allows you to easily convert a method that throws a checked exception into an instance compatible with a regular Supplier .

For example:

optional.orElseGet(ThrowingSupplier.of(Example::methodThatCanThrowCheckedException, IllegalStateException::new));