title: CompletionStage and exception handling
body: <p>I've been trying to understand how to properly handle exceptions when using CompletionStage, I've looked at the exceptionally() method but that is not good enough for my needs, I don't want to use it to intercept an exception thrown by an async method and pass along a new default value, I want it to intercept an exception, possibly break the chain and replace the current class of the output element of the chain with another one, in particular the output class of the main method.</p>
  
  <p>This is a sample code:</p>
  
  <pre><code>public CompletionStage&lt;Result&gt; getTableAsJSON(String instance, Http.Request request) {
          return this.repositoryManager.getRepository(instance).map(repo -&gt;
              repo.getTabularData(request.queryString()).exceptionally(ex -&gt; {
                  return new ArrayList&lt;&gt;();
              }).thenApplyAsync(list -&gt; {
                      if(!list.isEmpty())
                          return ok(toJson(list));
                      else
                          return notFound(this.messagesApi.preferred(request).at("noDataFound"));
                  }, this.httpExecutionContext.current()
              )        ).orElse(CompletableFuture.completedFuture(notFound(this.messagesApi.preferred(request).at("instanceNotFound"))));
      }
  </code></pre>
  
  <p>In the code above, if a SqlException is thrown inside getTabularData, exceptionally catches it and passes along an empty list because getTabularData's signature is </p>
  
  <pre><code>CompletionStage&lt;List&lt;?&gt;&gt; getTabularData(Map&lt;String, String[]&gt; parameters);
  </code></pre>
  
  <p>It's true that inside exceptionally I could differentiate between a SqlException or a NumberFormatException, but in the end it must pass along a List to the thenApplyAsync, and once the flow gets in there, it can't differenciate between what exception was raised and return a different Result accordingly.</p>
  
  <p>Is there a better way than using the old school try catch blocks to accomplish proper exception handling with CompletionStage?</p>
  
  <p>Thank you,
  Roberto</p>