Java中的CompletableFutures和异常处理

时间:2016-07-08 22:36:34

标签: java exception completable-future

我是整个CompletableFutures业务的新手。目前,我正在尝试使用从服务调用中检索的对象填充期货列表,然后返回列表本身。但是,我得到了 error: unreported exception InvalidRequestException; must be caught or declared to be thrownunreported 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());
}

知道我缺少什么吗?

0 个答案:

没有答案