如何处理java.util.concurrent.ExecutionException异常?
我的代码的某些部分正在抛出java.util.concurrent.ExecutionException异常。如何处理java.util.concurrent.ExecutionException异常?,java,error-handling,executionexception,Java,Error Handling,Executionexception,我的代码的某些部分正在抛出java.util.concurrent.ExecutionException异常。 我该怎么办?我可以使用throws子句吗?我对java有点陌生。您应该调查并处理ExecutionException的原因 一种可能性,如《Java并发操作》一书中所述,是创建launderThrowable方法来处理一般ExecutionExceptions void launderThrowable ( final Throwable ex ) { if ( ex inst
我该怎么办?我可以使用throws子句吗?我对java有点陌生。您应该调查并处理ExecutionException的原因 一种可能性,如《Java并发操作》一书中所述,是创建
launderThrowable
方法来处理一般ExecutionExceptions
void launderThrowable ( final Throwable ex )
{
if ( ex instanceof ExecutionException )
{
Throwable cause = ex.getCause( );
if ( cause instanceof RuntimeException )
{
// Do not handle RuntimeExceptions
throw cause;
}
if ( cause instanceof MyException )
{
// Intelligent handling of MyException
}
...
}
...
}
如果你想处理这个异常,事情很简单
public void exceptionFix1() {
try {
//code that throws the exception
} catch (ExecutionException e) {
//what to do when it throws the exception
}
}
public void exceptionFix2() throws ExecutionException {
//code that throws the exception
}
请记住,第二个示例必须包含在执行层次结构的某个位置的try-catch
块中
如果您希望修复此异常,我们将不得不查看您的更多代码。这取决于您的
未来
的任务关键性如何处理它。事实上,你不应该得到一个。只有在未处理的将来执行的代码抛出了某个异常时,才会出现此异常
当您catch(ExecutionException e)
时,您应该能够使用e.getCause()
来确定您的未来发生了什么
理想情况下,您的异常不会像这样浮出水面,而是直接在未来的中处理