Interface FunctionWithException<T, R, E extends Exception>

Type Parameters:
T - the input type
R - the result type
E - the checked exception type
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 FunctionWithException<T, R, E extends Exception>
A function whose operation may throw a checked exception, with adapters for APIs that accept ordinary functions.
Since:
3.31
Author:
Eike Stepper
  • Method Details

    • apply

      R apply(T value) throws E
      Applies this function to an input value.
      Parameters:
      value - the input value
      Returns:
      the function result
      Throws:
      E - if the operation fails
    • function

      static <T, R, E extends Exception> Function<T,R> function(FunctionWithException<T,R,E> function)
      Adapts a checked-exception function to a regular Function. Any caught exception is wrapped in a RuntimeException.
      Parameters:
      function - the checked-exception function
      Returns:
      a function that wraps checked exceptions
    • function

      static <T, R, E extends Exception> Function<T,R> function(FunctionWithException<T,R,E> function, Class<E> exceptionClass, Consumer<E> exceptionHandler)
      Adapts a checked-exception function to a regular Function. If an exception handler is supplied, exceptions matching exceptionClass are passed to it. As with ConsumerWithException, the original exception is then wrapped in a RuntimeException; exceptions of other types skip the handler and are wrapped directly.
      Parameters:
      function - the checked-exception function
      exceptionClass - the exception type accepted by the handler, or null when no handler is used
      exceptionHandler - the optional handler for the selected exception type
      Returns:
      a function that handles matching exceptions and wraps them