Java : ExecutionException 示例
Java 中的 ExecutionException (Java SE 23 & JDK 23) 及其示例。
您将找到大多数 ExecutionException 方法的代码示例。
注解 :
- 本文可能使用了翻译软件以方便阅读。 另请查看英文原文。
简介
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
请参阅下面的链接。
相关文章
- API 示例