MST

星途 面试题库

面试题:Java中CompletableFuture如何处理异常

在Java的CompletableFuture异步编程中,假设你有一个CompletableFuture任务链,其中某个任务可能会抛出异常。请描述如何捕获并处理这些异常,同时给出相应的代码示例。
13.4万 热度难度
编程语言Java

知识考点

AI 面试

面试题答案

一键面试
  1. 异常捕获与处理方式

    • 在CompletableFuture任务链中,可以使用exceptionally方法来捕获并处理异常。exceptionally方法接收一个Function,该Function的输入是异常,返回值是在出现异常时要返回的结果。
    • 还可以使用whenCompletewhenCompleteAsync方法,它们会在任务完成(包括正常完成和异常完成)时执行,但不会改变任务的返回结果。如果需要在异常时返回特定结果,还是需要结合exceptionally方法。
    • 另外,handlehandleAsync方法也可以处理异常,它们接收一个BiFunction,第一个参数是正常的结果(如果有),第二个参数是异常(如果有),返回值是处理后的结果。
  2. 代码示例

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class CompletableFutureExceptionHandling {
    public static void main(String[] args) {
        CompletableFuture.supplyAsync(() -> {
            if (Math.random() > 0.5) {
                throw new RuntimeException("模拟异常");
            }
            return "任务正常结果";
        })
       .exceptionally(ex -> {
            System.out.println("捕获到异常: " + ex.getMessage());
            return "异常时的替代结果";
        })
       .thenAccept(System.out::println);

        // 使用handle方法的示例
        CompletableFuture.supplyAsync(() -> {
            if (Math.random() > 0.5) {
                throw new RuntimeException("模拟异常");
            }
            return "任务正常结果";
        })
       .handle((result, ex) -> {
            if (ex != null) {
                System.out.println("handle捕获到异常: " + ex.getMessage());
                return "handle异常时的替代结果";
            }
            return result;
        })
       .thenAccept(System.out::println);

        try {
            // 获取最终结果,如果异常未处理,这里会抛出异常
            String result = CompletableFuture.supplyAsync(() -> {
                if (Math.random() > 0.5) {
                    throw new RuntimeException("模拟异常");
                }
                return "任务正常结果";
            })
           .get();
            System.out.println("最终结果: " + result);
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
    }
}

上述代码中,通过CompletableFuture.supplyAsync创建异步任务,然后使用exceptionallyhandle方法分别展示了捕获和处理异常的方式。thenAccept用于接收最终的结果并打印。同时也展示了通过get方法获取结果时,如果异常未在前面处理,这里会抛出异常的情况。