title: How to safely unwrap a CompletableException
body: <p>I'm trying to figure out a way in which when working with <code>CompletableFuture</code> to safely and without repeating the same code throughout my codebase, handle the <code>CompletionException</code> thrown when calling <code>CompletableFuture.join()</code>.</p>
  
  <p>The behavior I'm looking for is - If the cause of the exception is any <strong>unchecked</strong> exception, then it should be thrown as-is. If the cause is <strong>checked</strong>, then wrap it in some form of <code>RuntimeException</code> (I've used the base <code>RuntimeException</code> class for this example, but in a real application I'd use a custom derived class) and throw the wrapper exception.</p>
  
  <p>So far, I've come up with the following utility function to do the unwrapping:</p>
  
  <pre><code>public static RuntimeException unwrapCompletionException(CompletionException e) {
      Throwable cause =  e.getCause();
      if (cause instanceof RuntimeException) {
          return (RuntimeException) cause;
      } else if (cause instanceof InterruptedException) {
          Thread.currentThread().interrupt();
          return new RuntimeException(e.getCause());
      } else {
          // Any other checked exception
          return new RuntimeException(e.getCause());
      }
  }
  </code></pre>
  
  <p>And for example, the utility function could be used as so:</p>
  
  <pre><code>try {
      completableFuture.join();
  } catch (CompletionException e) {
      throw unwrapCompletionException(e);
  } catch (CancellationException e) {
      // Handled separately...
  }
  </code></pre>
  
  <p>My question is about the safety of this approach and the completeness of the unwrapping code. Like for example, <code>InterruptedException</code> was one special case I could think of to handle, but maybe others exist as well. Maybe there are other consequences I didn't consider. Is my utility function missing anything in order to do what I'm intending when handling <code>CompletionException</code> in any circumstance?</p>