当我使用Void作为返回类型时,我想知道如何处理可调用的异常。
就像这样:
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(new Callable<Void>() {
@Override
public Void call() throws Exception {
call a third party code where an exception is thrown...
return null;
}
});由于我不需要这个可调用的结果,所以我不调用executor.submit()返回的未来的get()。这样,例外就被吞没了。
处理这些例外情况的正确方法是什么?
发布于 2014-07-31 09:05:01
在调用()方法中尝试/捕捉怎么样?-我想这取决于发生异常时您想要做什么。
public Void call() throws Exception {
try {
ThirdParty.doSomething();
} catch(SomeTypeException e) {
SomeErrorHandler.handleThisError(e); // E.g. report it to the user
}
return null;
}https://stackoverflow.com/questions/25054759
复制相似问题