Interface CatchFunction<R,E extends Throwable>
- Type Parameters:
R- the type of the result of the asynchronous operationE- the type of the exception to catch, extendingThrowable
- All Superinterfaces:
Async<R>
- All Known Subinterfaces:
AsyncCatchFunction<R,E>
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
CatchFunction interface represents a function that handles exceptions
occurring within an asynchronous operation. It provides a mechanism to catch and
process exceptions to a specific type, allowing for error recovery or alternative
processing paths within an asynchronous workflow.
This interface is part of the asynchronous utilities provided by the Hadoop
Distributed File System (HDFS) Federation router. It is used in conjunction with
other asynchronous interfaces such as AsyncRun and
FinallyFunction to build complex, non-blocking operations.
An implementation of this interface should define how to handle a caught exception. It takes two parameters: the result of the asynchronous operation (if any) and the caught exception. The function can then return a new result or modify the existing result to continue the asynchronous processing.
CatchFunction is used to implement the following semantics:
try{
R res = doAsync(input);
} catch(E e) {
// Can use CatchFunction
R result = thenApply(res, e);
}
- See Also:
-
Field Summary
Fields inherited from interface org.apache.hadoop.hdfs.server.federation.router.async.utils.Async
CUR_COMPLETABLE_FUTURE -
Method Summary
Methods inherited from interface org.apache.hadoop.hdfs.server.federation.router.async.utils.Async
getCurCompletableFuture, result, setCurCompletableFuture
-
Method Details
-
apply
Applies this catch function to the given result and exception.This method is called to process an exception that occurred during an asynchronous operation. The implementation of this method should define how to handle the caught exception. It may involve logging the error, performing a recovery operation, or any other custom error handling logic.
The method takes two parameters: the result of the asynchronous operation (if any), and the caught exception. Depending on the implementation, the method may return a new result, modify the existing result, or throw a new exception.
- Parameters:
r- the result of the asynchronous operation, which may be null if the operation did not complete successfullye- the caught exception, which the function should handle- Returns:
- the result after applying the catch function, which may be a new result or a modified version of the input result
- Throws:
IOException- if an I/O error occurs during the application of the catch function
-
apply
Applies the catch function to aCompletableFuture, handling exceptions of a specified type.This default method provides a way to integrate exception handling into a chain of asynchronous operations. It takes a
CompletableFutureand a class object representing the type of exception to catch. The method uses the handle method of theCompletableFutureto apply the catch function.If the input future completes exceptionally with an instance of the specified exception type, the catch function is applied to the exception. If the future completes with a different type of exception or normally, the original result or exception is propagated.
- Parameters:
in- the inputCompletableFutureto which the catch function is appliedeClazz- the class object representing the exception type to catch- Returns:
- a new
CompletableFuturethat completes with the result of applying the catch function, or propagates the original exception if it does not match the specified type
-