Java : ExecutionException with Examples
ExecutionException (Java SE 23 & JDK 23) in Java with Examples.
You will find code samples for most of the ExecutionException methods.
Summary
try (final var executor = Executors.newSingleThreadExecutor()) {
final var future = executor.submit(() -> {
System.out.println("task : start");
throw new IllegalStateException("task error!");
});
future.get();
} catch (ExecutionException e) {
System.out.println("ExecutionException! : " + e.getCause());
}
// Result
// ↓
//task : start
//ExecutionException! : java.lang.IllegalStateException: task error!
Constructors
ExecutionException ()
protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
ExecutionException (String message)
protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
ExecutionException (String message, Throwable cause)
final var cause = new IOException("XYZ");
final var e = new ExecutionException("abcd", cause);
System.out.println(e); // java.util.concurrent.ExecutionException: abcd
System.out.println(e.getMessage()); // abcd
System.out.println(e.getCause()); // java.io.IOException: XYZ
System.out.println(e.getCause().getMessage()); // XYZ
ExecutionException (Throwable cause)
final var cause = new IOException("XYZ");
final var e = new ExecutionException(cause);
// java.util.concurrent.ExecutionException: java.io.IOException: XYZ
System.out.println(e);
System.out.println(e.getCause()); // java.io.IOException: XYZ
System.out.println(e.getCause().getMessage()); // XYZ
Methods declared in Throwable
addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString
Please see the link below.
Related posts
- API Examples
- BlockingQueue
- Callable
- CancellationException
- ConcurrentHashMap.KeySetView
- ConcurrentLinkedDeque
- ConcurrentLinkedQueue
- ConcurrentMap
- ConcurrentModificationException
- ConcurrentSkipListSet
- Condition
- CopyOnWriteArrayList
- CopyOnWriteArraySet
- CountDownLatch
- CyclicBarrier
- Exchanger
- Executor
- ExecutorService
- Executors
- Future
- Future.State
- FutureTask
- InterruptedException
- Lock
- Runnable
- Semaphore
- Thread
- ThreadGroup
- ThreadLocal
- TimeUnit