Java Thread Future unwrap(Throwable throwable, Class throwableA)

Here you can find the source of unwrap(Throwable throwable, Class throwableA)

Description

Unwraps ExecutionException thrown from a CompletableFuture.

License

Apache License

Parameter

Parameter Description
throwable Throwable to unwrap.
throwableA Checked Exception to expose.
A Class of checked exception.

Exception

Parameter Description
A Throws checked exception.

Declaration

public static <A extends Throwable> void unwrap(Throwable throwable, Class<A> throwableA) throws A 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutionException;

public class Main {
    /**/*from ww w .  j a  v a2 s .co m*/
     * Unwraps ExecutionException thrown from a CompletableFuture.
     *
     * @param throwable  Throwable to unwrap.
     * @param throwableA Checked Exception to expose.
     * @param <A>        Class of checked exception.
     * @throws A Throws checked exception.
     */
    public static <A extends Throwable> void unwrap(Throwable throwable, Class<A> throwableA) throws A {

        Throwable unwrapThrowable = throwable;
        if (throwable instanceof ExecutionException || throwable instanceof CompletionException) {
            unwrapThrowable = throwable.getCause();
        }

        if (throwableA.isInstance(unwrapThrowable)) {
            throw (A) unwrapThrowable;
        }
        if (unwrapThrowable instanceof RuntimeException) {
            throw (RuntimeException) unwrapThrowable;
        }
        if (unwrapThrowable instanceof Error) {
            throw (Error) unwrapThrowable;
        }
        throw new RuntimeException(unwrapThrowable);
    }

    /**
     * Unwraps ExecutionException thrown from a CompletableFuture.
     *
     * @param throwable Throwable to unwrap.
     */
    public static void unwrap(Throwable throwable) {
        unwrap(throwable, RuntimeException.class);
    }
}

Related

  1. sequence(List> futures)
  2. submitTasks(ExecutorCompletionService ecs, Iterable> tasks)
  3. sumFutures(List> futures)
  4. transferResult(final CompletionStage source, final CompletableFuture target)
  5. transferResult(final CompletionStage source, final CompletableFuture target)
  6. waitFor(Future future)
  7. waitForAll(List> futures)
  8. waitForAll(List> res)
  9. waitForCompletion(Collection> futures)