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 functional interface representing an operation that accepts a single argument, returns no result, and may throw a checked exception.

This interface extends the standard Java Consumer to allow the functional method to throw checked exceptions. The default accept(Object) method wraps any checked exceptions in a RuntimeException, making it compatible with standard Consumer usage while still allowing the implementation to throw checked exceptions via acceptThrows(Object).

Features:
  • Functional interface - can be used with lambda expressions and method references
  • Exception support - allows checked exceptions to be thrown via acceptThrows(Object)
  • Compatible with Consumer - implements Consumer.accept(Object) by wrapping exceptions
  • Dual interface - can be used as both Consumer and ThrowingConsumer
Use Cases:
  • Operations that may throw I/O exceptions (file writing, network operations)
  • Validation operations that may throw validation exceptions
  • Side-effect operations that may fail with checked exceptions
  • Operations that need to propagate checked exceptions
Usage:

// Lambda with exception ThrowingConsumer<String> fileWriter = (content) -> { Files.write(Paths.get("output.txt"), content.getBytes()); }; // Using acceptThrows to get checked exception try { fileWriter.acceptThrows("Hello World"); } catch (IOException e) { // Handle checked exception } // Using accept wraps exception in RuntimeException fileWriter.accept("Hello World"); // May throw RuntimeException

Exception Handling:
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    default void
    accept(T t)
    Performs this operation on the given argument, wrapping any checked exceptions in a RuntimeException.
    void
    Performs this operation on the given argument, potentially throwing a checked exception.

    Methods inherited from interface java.util.function.Consumer

    andThen
  • Method Details

    • accept

      default void accept(T t)
      Performs this operation on the given argument, wrapping any checked exceptions in a RuntimeException.

      This is the default implementation that makes this interface compatible with Consumer. It calls acceptThrows(Object) and wraps any checked exceptions.

      Specified by:
      accept in interface Consumer<T>
      Parameters:
      t - The input argument.
      Throws:
      RuntimeException - if acceptThrows(Object) throws a checked exception.
    • acceptThrows

      void acceptThrows(T t) throws Exception
      Performs this operation on the given argument, potentially throwing a checked exception.

      This is the functional method that implementations must provide. It allows checked exceptions to be thrown directly, unlike the standard Consumer.accept(Object) method.

      Example:

      ThrowingConsumer<String> validator = (value) -> { if (value == null || value.isEmpty()) { throw new ValidationException("Value cannot be empty"); } }; try { validator.acceptThrows(""); } catch (ValidationException e) { // Handle checked exception }

      Parameters:
      t - The input argument.
      Throws:
      Exception - If an error occurs during operation execution.