ThrowingConsumer

A Consumer that allows invocation of code that throws a checked Exception .

API

ThrowingConsumer.java
interface ThrowingConsumer<T> {
  void acceptWithException(T t)     (1)
  void accept(T t)     (2)
  void accept(T t, BiFunction<String, Exception, RuntimeException> exceptionWrapper)     (3)
  ThrowingConsumer<T> throwing(BiFunction<String, Exception, RuntimeException> exceptionWrapper)     (4)
  ThrowingConsumer<T> of(ThrowingConsumer<T> consumer)     (5)
  ThrowingConsumer<T> of(ThrowingConsumer<T> consumer, BiFunction<String, Exception, RuntimeException> exceptionWrapper)     (6)
}
1 acceptWithException(T)

Performs this operation on the given argument, possibly throwing a checked exception.

2 accept(T)

Default Consumer#accept(Object) that wraps any thrown checked exceptions (by default in a RuntimeException ).

3 accept(T, BiFunction)

Performs this operation on the given argument, wrapping any thrown checked exceptions using the given exceptionWrapper .

4 throwing(BiFunction)

Return a new ThrowingConsumer where the #accept(Object) method wraps any thrown checked exceptions using the given exceptionWrapper .

5 of(ThrowingConsumer)

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

6 of(ThrowingConsumer, BiFunction)

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

Members

acceptWithException(T)

Performs this operation on the given argument, possibly throwing a checked exception.

accept(T)

Default Consumer#accept(Object) that wraps any thrown checked exceptions (by default in a RuntimeException ).

accept(T, BiFunction)

Performs this operation on the given argument, wrapping any thrown checked exceptions using the given exceptionWrapper .

throwing(BiFunction)

Return a new ThrowingConsumer where the #accept(Object) method wraps any thrown checked exceptions using the given exceptionWrapper .

of(ThrowingConsumer)

Lambda friendly convenience method that can be used to create a ThrowingConsumer where the #accept(Object) 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 Consumer .

For example:

list.forEach(ThrowingConsumer.of(Example::methodThatCanThrowCheckedException));

of(ThrowingConsumer, BiFunction)

Lambda friendly convenience method that can be used to create a ThrowingConsumer where the #accept(Object) 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 Consumer .

For example:

list.forEach(ThrowingConsumer.of(Example::methodThatCanThrowCheckedException, IllegalStateException::new));