Interface ThrowingConsumer<T>

Type Parameters:
T - the type of the input to the operation
All Superinterfaces:
Consumer<T>
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface ThrowingConsumer<T> extends Consumer<T>
A Consumer that allows invocation of code that throws a checked exception.
Since:
6.0
Author:
Stephane Nicoll, Phillip Webb
  • Method Details

    • acceptWithException

      void acceptWithException(T t) throws Exception
      Performs this operation on the given argument, possibly throwing a checked exception.
      Parameters:
      t - the input argument
      Throws:
      Exception - on error
    • accept

      default void accept(T t)
      Default Consumer.accept(Object) that wraps any thrown checked exceptions (by default in a RuntimeException).
      Specified by:
      accept in interface Consumer<T>
      See Also:
    • accept

      default void accept(T t, BiFunction<String,Exception,RuntimeException> exceptionWrapper)
      Performs this operation on the given argument, wrapping any thrown checked exceptions using the given exceptionWrapper.
      Parameters:
      exceptionWrapper - BiFunction that wraps the given message and checked exception into a runtime exception
    • throwing

      default ThrowingConsumer<T> throwing(BiFunction<String,Exception,RuntimeException> exceptionWrapper)
      Return a new ThrowingConsumer where the accept(Object) method wraps any thrown checked exceptions using the given exceptionWrapper.
      Parameters:
      exceptionWrapper - BiFunction that wraps the given message and checked exception into a runtime exception
      Returns:
      the replacement ThrowingConsumer instance
    • of

      static <T> ThrowingConsumer<T> of(ThrowingConsumer<T> consumer)
      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));
       
      Type Parameters:
      T - the type of the input to the operation
      Parameters:
      consumer - the source consumer
      Returns:
      a new ThrowingConsumer instance
    • of

      static <T> ThrowingConsumer<T> of(ThrowingConsumer<T> consumer, BiFunction<String,Exception,RuntimeException> exceptionWrapper)
      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));
       
      Type Parameters:
      T - the type of the input to the operation
      Parameters:
      consumer - the source consumer
      exceptionWrapper - the exception wrapper to use
      Returns:
      a new ThrowingConsumer instance