Interface ThrowingConsumer2<A,B>

Type Parameters:
A - The type of the first argument to the operation.
B - The type of the second argument to the operation.
All Superinterfaces:
Consumer2<A,B>
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 ThrowingConsumer2<A,B> extends Consumer2<A,B>
A functional interface representing an operation that accepts two arguments, returns no result, and may throw a checked exception.

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

Features:
  • Functional interface - can be used with lambda expressions and method references
  • Exception support - allows checked exceptions to be thrown via acceptThrows(Object, Object)
  • Compatible with Consumer2 - implements Consumer2.apply(Object, Object) by wrapping exceptions
  • Dual interface - can be used as both Consumer2 and ThrowingConsumer2
Use Cases:
  • Two-argument operations that may throw I/O exceptions
  • 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 ThrowingConsumer2<String,Integer> fileWriter = (path, content) -> { Files.write(Paths.get(path), content.getBytes()); }; // Using acceptThrows to get checked exception try { fileWriter.acceptThrows("/tmp/output.txt", "Hello World"); } catch (IOException e) { // Handle checked exception } // Using apply wraps exception in RuntimeException fileWriter.apply("/tmp/output.txt", "Hello World"); // May throw RuntimeException

Exception Handling:
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    acceptThrows(A a, B b)
    Performs this operation on the given arguments, potentially throwing a checked exception.
    andThen(ThrowingConsumer2<? super A,? super B> after)
    Returns a composed ThrowingConsumer2 that performs, in sequence, this operation followed by the after operation.
    default void
    apply(A a, B b)
    Performs this operation on the given arguments, wrapping any checked exceptions in a RuntimeException.

    Methods inherited from interface org.apache.juneau.commons.function.Consumer2

    andThen
  • Method Details

    • apply

      default void apply(A a, B b)
      Performs this operation on the given arguments, wrapping any checked exceptions in a RuntimeException.

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

      Specified by:
      apply in interface Consumer2<A,B>
      Parameters:
      a - The first operation argument.
      b - The second operation argument.
      Throws:
      RuntimeException - if acceptThrows(Object, Object) throws a checked exception.
    • andThen

      default ThrowingConsumer2<A,B> andThen(ThrowingConsumer2<? super A,? super B> after)
      Returns a composed ThrowingConsumer2 that performs, in sequence, this operation followed by the after operation.

      This method enables operation composition, allowing you to chain multiple operations together. If performing either operation throws an exception, it is relayed to the caller of the composed operation. If performing this operation throws an exception, the after operation will not be performed.

      Parameters:
      after - The operation to perform after this operation. Must not be null.
      Returns:
      A composed ThrowingConsumer2 that performs in sequence this operation followed by the after operation.
      Throws:
      NullPointerException - if after is null.
    • acceptThrows

      void acceptThrows(A a, B b) throws Exception
      Performs this operation on the given arguments, 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 Consumer2.apply(Object, Object) method.

      Example:

      ThrowingConsumer2<String,Integer> validator = (name, age) -> { if (name == null || name.isEmpty()) { throw new ValidationException("Name cannot be empty"); } if (age < 0) { throw new ValidationException("Age cannot be negative"); } }; try { validator.acceptThrows("", -1); } catch (ValidationException e) { // Handle checked exception }

      Parameters:
      a - The first operation argument.
      b - The second operation argument.
      Throws:
      Exception - If an error occurs during operation execution.