Interface ThrowingFunction<T,R>

Type Parameters:
T - The type of the input to the function.
R - The type of the result of the function.
All Superinterfaces:
Function<T,R>
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 ThrowingFunction<T,R> extends Function<T,R>
A functional interface representing a function that accepts one argument, produces a result, and may throw a checked exception.

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

Features:
  • Functional interface - can be used with lambda expressions and method references
  • Exception support - allows checked exceptions to be thrown via applyThrows(Object)
  • Compatible with Function - implements Function.apply(Object) by wrapping exceptions
  • Dual interface - can be used as both Function and ThrowingFunction
Use Cases:
  • Transformations that may throw I/O exceptions
  • Parsing operations that may throw validation exceptions
  • Data conversion that may fail with checked exceptions
  • Operations that need to propagate checked exceptions
Usage:

// Lambda with exception ThrowingFunction<String,File> fileParser = (path) -> { if (! Files.exists(Paths.get(path))) { throw new FileNotFoundException("File not found: " + path); } return new File(path); }; // Using applyThrows to get checked exception try { File file = fileParser.applyThrows("/path/to/file"); } catch (FileNotFoundException e) { // Handle checked exception } // Using apply wraps exception in RuntimeException File file = fileParser.apply("/path/to/file"); // May throw RuntimeException

Exception Handling:
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    default R
    apply(T t)
    Applies this function to the given argument, wrapping any checked exceptions in a RuntimeException.
    Applies this function to the given argument, potentially throwing a checked exception.

    Methods inherited from interface java.util.function.Function

    andThen, compose
  • Method Details

    • apply

      default R apply(T t)
      Applies this function to the given argument, wrapping any checked exceptions in a RuntimeException.

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

      Specified by:
      apply in interface Function<T,R>
      Parameters:
      t - The function argument.
      Returns:
      The function result.
      Throws:
      RuntimeException - if applyThrows(Object) throws a checked exception.
    • applyThrows

      R applyThrows(T t) throws Exception
      Applies this function to 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 Function.apply(Object) method.

      Example:

      ThrowingFunction<String,Integer> parser = (s) -> { try { return Integer.parseInt(s); } catch (NumberFormatException e) { throw new ParseException("Invalid number: " + s, 0); } }; try { int value = parser.applyThrows("123"); } catch (ParseException e) { // Handle checked exception }

      Parameters:
      t - The function argument.
      Returns:
      The function result.
      Throws:
      Exception - If an error occurs during function execution.