Example usage for java.util.concurrent ExecutionException getCause

List of usage examples for java.util.concurrent ExecutionException getCause

Introduction

In this page you can find the example usage for java.util.concurrent ExecutionException getCause.

Prototype

public synchronized Throwable getCause() 

Source Link

Document

Returns the cause of this throwable or null if the cause is nonexistent or unknown.

Usage

From source file:Main.java

public static void main(String[] args) {
    Callable<Object> badTask = () -> {
        throw new RuntimeException("Throwing exception from task execution...");
    };/* ww  w .j  a  va2  s  .c o m*/
    ExecutorService exec = Executors.newSingleThreadExecutor();
    Future submittedTask = exec.submit(badTask);
    try {
        Object result = submittedTask.get();
    } catch (ExecutionException e) {
        System.out.println(e.getMessage());
        System.out.println(e.getCause().getMessage());
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    exec.shutdown();
}

From source file:Main.java

public static void runInNewThread(String threadName, Runnable target) throws Throwable {
    FutureTask<Object> future = new FutureTask<Object>(target, null);
    new Thread(future, threadName).start();
    try {//w w w. j av a2s. c  o m
        future.get();
    } catch (ExecutionException e) {
        throw e.getCause();
    }
}

From source file:Main.java

/**
 * Tests whether the cause of the specified {@code ExecutionException}
 * should be thrown and does it if necessary.
 *
 * @param ex the exception in question/*  w w  w.  ja va 2s.co m*/
 */
private static void throwCause(ExecutionException ex) {
    if (ex.getCause() instanceof RuntimeException) {
        throw (RuntimeException) ex.getCause();
    }

    if (ex.getCause() instanceof Error) {
        throw (Error) ex.getCause();
    }
}

From source file:Main.java

private static void throwCause(ExecutionException ex)
/*  54:    */ {/*from w  w w  . j  ava  2s  .c om*/
    /*  55:159 */if ((ex.getCause() instanceof RuntimeException)) {
        /*  56:160 */throw ((RuntimeException) ex.getCause());
        /*  57:    */}
    /*  58:163 */if ((ex.getCause() instanceof Error)) {
        /*  59:164 */throw ((Error) ex.getCause());
        /*  60:    */}
    /*  61:    */}

From source file:Main.java

/**
 * Simple helper method to have timed run method (that timeouts if runs too
 * long). Good for long calculations wich has time limit.
 * /*w  ww .jav  a 2 s .c  o  m*/
 * @param <V>
 * @param c Callable with calculation logic.
 * @param timeout Time after calculation times out. 
 * @param timeUnit Time metric (seconds, minutes etc).
 * 
 * @return Calculation result if calculation finished.
 * 
 * @throws Throwable Exception that might be thrown in calculation.
 */
public static <V> V timedRun(Callable<V> c, long timeout, TimeUnit timeUnit) throws Throwable {
    Future<V> task = taskExec.submit(c);
    try {
        return task.get(timeout, timeUnit);
    } catch (ExecutionException e) {
        throw e.getCause();
    } catch (TimeoutException e) {
        // throw exception if need to know that timeout occured
        // or leave empty if null can be returned on timeout
    } finally {
        task.cancel(true);
    }
    return null;
}

From source file:Main.java

/**
 * Verify that no assertions have failed inside a future.
 * Used for unit tests that spawn threads. E.g.,
 * <p>/* ww w  . ja va  2 s .  c o m*/
 * <code>
 *   List<Future<Void>> results = Lists.newArrayList();
 *   Future<Void> f = executor.submit(new Callable<Void> {
 *     public Void call() {
 *       assertTrue(someMethod());
 *     }
 *   });
 *   results.add(f);
 *   assertOnFutures(results);
 * </code>
 * @param threadResults A list of futures
 * @param <T>
 * @throws InterruptedException If interrupted when waiting for a result
 *                              from one of the futures
 * @throws ExecutionException If an exception other than AssertionError
 *                            occurs inside any of the futures
 */
public static <T> void assertOnFutures(List<Future<T>> threadResults)
        throws InterruptedException, ExecutionException {
    for (Future<T> threadResult : threadResults) {
        try {
            threadResult.get();
        } catch (ExecutionException e) {
            if (e.getCause() instanceof AssertionError) {
                throw (AssertionError) e.getCause();
            }
            throw e;
        }
    }
}

From source file:Main.java

public static <R> R runOnMainSyncAndGetResult(Instrumentation instrumentation, Callable<R> callable)
        throws Throwable {
    FutureTask<R> task = new FutureTask<R>(callable);
    instrumentation.runOnMainSync(task);
    try {/*from  w ww.j  a v a2s .c  o  m*/
        return task.get();
    } catch (ExecutionException e) {
        // Unwrap the cause of the exception and re-throw it.
        throw e.getCause();
    }
}

From source file:co.paralleluniverse.fibers.okhttp.FiberOkHttpUtils.java

private static <X> X fiberOkTry(final SuspendableCallable<X> call)
        throws InterruptedException, IOException, ExecutionException {
    X res = null;//from   w ww  .j  a  v a2  s.  co m
    try {
        res = new Fiber<>(call).start().get();
    } catch (ExecutionException ee) {
        if (ee.getCause() instanceof RuntimeException) {
            final RuntimeException re = (RuntimeException) ee.getCause();
            if (re.getCause() instanceof IOException)
                throw (IOException) re.getCause();
            else
                throw re;
        } else if (ee.getCause() instanceof IllegalStateException)
            throw ((IllegalStateException) ee.getCause());
        else
            throw ee;
    }
    // Should never happen
    return res;
}

From source file:org.apache.bookkeeper.stream.segment.BKSegmentTestCase.java

static void assertFuture(OrderingListenableFuture<SSN> future, Class<? extends Exception> expectedClass)
        throws InterruptedException {
    try {//from  ww w .java 2  s .  com
        future.get();
        fail("Should cancel the operation since the writer is already in error state");
    } catch (ExecutionException ee) {
        Throwable cause = ee.getCause();
        assertEquals(expectedClass, cause.getClass());
    }
}

From source file:org.apache.bookkeeper.stream.segment.BKSegmentTestCase.java

static void assertFuture(OrderingListenableFuture<SSN> future, int expectedRc) throws InterruptedException {
    try {//from   w  w  w .  ja va  2  s  . com
        future.get();
        fail("Should fail the operation since ledger handle is fenced");
    } catch (ExecutionException ee) {
        Throwable cause = ee.getCause();
        assertEquals(BKException.class, cause.getClass());
        BKException bke = (BKException) cause;
        assertEquals(expectedRc, bke.getBkCode());
    }
}