0

I'm new to the whole CompletableFutures business. Currently, I'm trying to populate a futures list with objects retrieved from a service call and then return the list itself. However, I'm getting error: unreported exception InvalidRequestException; must be caught or declared to be thrown and unreported exception DependencyFailureException; must be caught or declared to be thrown errors even though I'm using a try/catch block and declare the exceptions. Here's what I have so far:

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());
}

Any idea what I'm missing?

wonderv
  • 143
  • 1
  • 7
  • Presumably your `InvalidRequestException` and `DependencyFailureException` types are checked. The `Supplier` functional interface does not declare throwing any checked exceptions, let alone your custom ones. You'll need to handle them internally or rethrow them wrapped in an unchecked exception type. – Sotirios Delimanolis Jul 08 '16 at 22:40
  • Sotirios Delimanolis, do you have an example of how to go about doing something like that? – wonderv Jul 08 '16 at 23:48
  • An example of what? Handling is up to you, whatever process you've setup in your application. Wrapping in an uncheckedexception is also discussed [here](http://stackoverflow.com/questions/484794/wrapping-a-checked-exception-into-an-unchecked-exception-in-java). – Sotirios Delimanolis Jul 09 '16 at 00:48

0 Answers0