如何组合一系列例外?

Kai*_*Kai 5 java exception-handling exception guava

有什么方法可以合并一个扔掉的名单吗?我有一个ListenableFutures列表,每个都会捕获异常(如果有的话)并将其保存在Result实例中.最后,如果ListenableFutures的任何Result包含throwable,我想抛出异常.问题是如何组合多个throwable,因为可能有多个未来失败?

List<ListenableFuture<Result>> futureList = new ArrayList<>();
futureList.add(future1);
futureList.add(future2);
futureList.add(future3);

ListenableFuture<List<Result>> future = futureFutures.successfulAsList(futureList);

return Futures.transform(future, new Function<List<Result>, FinalResult>() {
      @Override
      public FinalResult apply(List<Result> resultList) {
        List<Throwable> throwableList = new ArrayList<>();
        for (Result result : resultList) {
            if (result.getThrowable() != null) {
                throwableList.add(result.getThowable());
            }
        }

        if (!throwableList.isEmpty()) {
           // Is there something like below that I can combine a list
           // of throwables and throw it?
           Throwable.propagateList(throwableList); // ?????????
        }
        .....
        return finalResult;
      }
    },
    MoreExecutors.sameThreadExecutor());
Run Code Online (Sandbox Code Playgroud)

Pau*_*cks 9

没有例外意味着"多件事出了问题",但没有什么可以阻止你创建一个.例外只是对象,它们可以有成员,包括类似的东西List<Throwable> getCauses().