我是整个CompletableFutures业务的新手。目前,我正在尝试使用从服务调用中检索的对象填充期货列表,然后返回列表本身。但是,我得到了
error: unreported exception InvalidRequestException; must be caught or declared to be thrown
和unreported exception DependencyFailureException; must be caught or declared to be thrown
错误,即使我正在使用try / catch块并声明异常。
这是我到目前为止所做的:
public List<Dog> getDogs(List<String> tagIds, String ownerId)
throws InvalidRequestException, DependencyFailureException {
List<CompletableFuture<Dog>> futures = new ArrayList<>(tagIds.size());
List<Dog> responses = new ArrayList<>(tagIds.size());
for(String tagId : tagIds) {
futures.add(CompletableFuture.supplyAsync(() -> {
try {
return getDog(tagId, ownerId);
} catch (InvalidRequestException ire) {
throw new InvalidRequestException("An invalid request to getDog", ire);
} catch (DependencyFailureException dfe) {
throw new DependencyFailureException("A dependency failure occurred during a getDog call", dfe);
}
}));
}
return futures.stream()
.map(CompletableFuture::join)
.collect(Collectors.toList());
}
知道我缺少什么吗?