2012-07-25 104 views

回答

2

的护理如果你正在寻找处理异常,事情很简单。

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块你的执行层次。

如果您正在修复这个异常,我们将不得不查看更多的代码。

2

您应该调查并处理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 
     } 

     ... 
    } 

    ... 
} 
8

这取决于你的Future是如何处理它的关键。事实是你永远不应该得到一个。如果您的Future中执行的代码没有处理,则您只会遇到此异常。

当你catch(ExecutionException e)你应该能够使用e.getCause()来确定您的Future发生了什么。

理想情况下,您的例外不会像这样冒泡到表面,而是直接在您的Future中处理。