Interface CatchFunction<R,E extends Throwable>

Type Parameters:
R - the type of the result of the asynchronous operation
E - the type of the exception to catch, extending Throwable
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.

@FunctionalInterface public interface CatchFunction<R,E extends Throwable> extends Async<R>
The 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:
  • Method Details

    • apply

      R apply(R r, E e) throws IOException
      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 successfully
      e - 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

      default CompletableFuture<R> apply(CompletableFuture<R> in, Class<E> eClazz)
      Applies the catch function to a CompletableFuture, 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 CompletableFuture and a class object representing the type of exception to catch. The method uses the handle method of the CompletableFuture to 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 input CompletableFuture to which the catch function is applied
      eClazz - the class object representing the exception type to catch
      Returns:
      a new CompletableFuture that completes with the result of applying the catch function, or propagates the original exception if it does not match the specified type