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.
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:
Exception Handling:
applyThrows(Object)- Throws checked exceptions directlyapply(Object)- Wraps checked exceptions inRuntimeException- Use
applyThrowswhen you need to handle specific checked exceptions - Use
applywhen you want standard Function behavior
See Also:
ThrowingSupplier- Supplier that throws exceptionsThrowingConsumer- Consumer that throws exceptions- juneau-commons Basics
-
Method Summary
Modifier and TypeMethodDescriptiondefault RApplies this function to the given argument, wrapping any checked exceptions in aRuntimeException.applyThrows(T t) Applies this function to the given argument, potentially throwing a checked exception.
-
Method Details
-
apply
Applies this function to the given argument, wrapping any checked exceptions in aRuntimeException.This is the default implementation that makes this interface compatible with
Function. It callsapplyThrows(Object)and wraps any checked exceptions.- Specified by:
applyin interfaceFunction<T,R> - Parameters:
t- The function argument.- Returns:
- The function result.
- Throws:
RuntimeException- ifapplyThrows(Object)throws a checked exception.
-
applyThrows
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 (NumberFormatExceptione ) {throw new ParseException("Invalid number: " +s , 0); } };try {int value =parser .applyThrows("123" ); }catch (ParseExceptione ) {// Handle checked exception }- Parameters:
t- The function argument.- Returns:
- The function result.
- Throws:
Exception- If an error occurs during function execution.
-