Interface ThrowableBiFunction<T,​U,​R>

  • Type Parameters:
    T - the type of the first argument to the function
    U - the type of the second argument to the function
    R - the type of the result of the function
    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 ThrowableBiFunction<T,​U,​R>
    Represents a function that accepts two arguments and produces a result, which may throw a Throwable.

    This is a functional interface whose functional method is apply(Object, Object).

    Example Usage

    
     ThrowableBiFunction<Integer, Integer, Integer> divide = (a, b) -> {
         if (b == 0) throw new ArithmeticException("Division by zero");
         return a / b;
     };
    
     // Using execute with default exception handling
     Integer result = ThrowableBiFunction.execute(10, 0, divide);
     // This will internally handle the exception using DEFAULT_EXCEPTION_HANDLER and throw a RuntimeException
    
     // Custom Exception Handling Example:
     Integer resultWithCustomHandler = ThrowableBiFunction.execute(10, 0, divide, (first, second, error) -> {
         System.out.println("Error occurred: " + error.getMessage());
         return 0; // Default value on failure
     });
     
    Author:
    Mercy
    See Also:
    BiFunction, Throwable
    • Method Detail

      • apply

        R apply​(T first,
                U second)
         throws java.lang.Throwable
        Applies this function to the given argument.
        Parameters:
        first - the first argument to be applied for the function
        second - the second argument to be applied for the function
        Returns:
        the function result
        Throws:
        java.lang.Throwable - if met with any error
      • execute

        static <T,​U,​R> R execute​(T first,
                                             U second,
                                             ThrowableBiFunction<T,​U,​R> function)
                                      throws java.lang.NullPointerException
        Executes the given ThrowableBiFunction with the provided arguments using the default exception handler.

        If an exception is thrown during the execution of the function, it will be handled by the DEFAULT_EXCEPTION_HANDLER, which wraps the exception into a RuntimeException and throws it.

        Example Usage

        
         ThrowableBiFunction<Integer, Integer, Integer> divide = (a, b) -> {
             if (b == 0) throw new ArithmeticException("Division by zero");
             return a / b;
         };
        
         // Execution with default exception handling
         Integer result = ThrowableBiFunction.execute(10, 0, divide);
         // This will throw a RuntimeException due to division by zero
         
        Type Parameters:
        T - the type of the first argument
        U - the type of the second argument
        R - the type of the result
        Parameters:
        first - the first argument to apply to the function
        second - the second argument to apply to the function
        function - the function to execute; must not be null
        Returns:
        the result of the function after successful execution
        Throws:
        java.lang.NullPointerException - if the given function is null
      • execute

        static <T,​U,​R> R execute​(T first,
                                             U second,
                                             ThrowableBiFunction<T,​U,​R> function,
                                             ThrowableBiFunction.ExceptionHandler<T,​U,​R> exceptionHandler)
                                      throws java.lang.NullPointerException
        Executes the given ThrowableBiFunction with the provided arguments using a custom exception handler.

        If an exception is thrown during the execution of the function, it will be handled by the specified ThrowableBiFunction.ExceptionHandler, which provides custom logic to recover or respond to the error.

        Example Usage

        
         ThrowableBiFunction<Integer, Integer, Integer> divide = (a, b) -> {
             if (b == 0) throw new ArithmeticException("Division by zero");
             return a / b;
         };
        
         // Custom Exception Handling Example:
         Integer resultWithCustomHandler = ThrowableBiFunction.execute(10, 0, divide, (first, second, error) -> {
             System.out.println("Error occurred: " + error.getMessage());
             return 0; // Default value on failure
         });
         
        Type Parameters:
        T - the type of the first argument
        U - the type of the second argument
        R - the type of the result
        Parameters:
        first - the first argument to apply to the function
        second - the second argument to apply to the function
        function - the function to execute; must not be null
        exceptionHandler - the handler to manage exceptions thrown during execution; must not be null
        Returns:
        the result of the function after execution (either successful or recovered via exception handling)
        Throws:
        java.lang.NullPointerException - if either the function or exceptionHandler is null