如何合并异常列表?
How to combine a list of exceptions?
有什么方法可以合并可抛物列表吗?我有一个 ListenableFutures 列表,每个列表都会捕获异常(如果有的话)并将其保存在 Result 实例中。最后,如果 ListenableFutures 的任何结果包含可抛出的,我想抛出异常。问题是如何组合多个 throwable,因为可能有多个 futures 失败?
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());
毫无例外,这意味着 "multiple things went wrong",但没有什么能阻止您创建一个。异常只是对象,它们可以有成员,包括像 List<Throwable> getCauses()
.
这样的东西
有什么方法可以合并可抛物列表吗?我有一个 ListenableFutures 列表,每个列表都会捕获异常(如果有的话)并将其保存在 Result 实例中。最后,如果 ListenableFutures 的任何结果包含可抛出的,我想抛出异常。问题是如何组合多个 throwable,因为可能有多个 futures 失败?
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());
毫无例外,这意味着 "multiple things went wrong",但没有什么能阻止您创建一个。异常只是对象,它们可以有成员,包括像 List<Throwable> getCauses()
.