2012-07-25 5 views

답변

2

을 풀기 돌봐 launderThrowable 방법을 만드는 것입니다, 상황이 매우 간단하다 .

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의 원인을 조사하고 처리해야합니다. 책 "액션 자바 동시성"에 설명

하나의 가능성은, 당신은 예외를 처리하고자하는 경우 일반 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)Future에서 어떤 일이 있었는지 확인하려면 e.getCause()을 사용할 수 있어야합니다.

예외적으로 예외는 다음과 같이 표면에 맞춰지지 않고 Future에서 직접 처리됩니다.

관련 문제