【发布时间】:2018-01-22 16:33:15
【问题描述】:
我试图在一个单独的线程上运行的任务中抛出一个异常。然后我想在调用线程上捕获异常。请参阅下面的试用版:
当我现在运行代码时,它挂在“抛出新的 RuntimeException..”的行
Task calcTask = createCalcTask();
ExecutorService executor = Executors.newFixedThreadPool(1);
Future future = executor.submit(calcTask);
try {
future.get();
} catch (ExecutionException ex) {
ex.getCause().printStackTrace();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
public Task<Object> createCalcTask() {
return new Task<Object>() {
@Override
protected Object call() throws Exception {
throw new RuntimeException("testE");
}
};
}
【问题讨论】:
-
您的线程无法处理 generic
RuntimeException...