2015-01-12 2 views
0

다음 프로그램에서 main exception을 얻으려고하면 프로그램의 첫 번째 예외를 일으키는 예외를 의미하므로 다음과 같은 오류가 발생합니다. cannot find symbol : e.getCause();이 문을 제거한 후 System.out.println("Main Cause : " + e.getCause()); 성공적으로 가져옵니다. 첫 번째 예외.연결 된 예외에서 심볼을 찾을 수 없음

class chain_demo { 
    static void demo() { 
     //create an exception 
     ArithmeticException e = new ArithmeticException("top Module"); 

     //cuase an exception 
     e.initCause(new NullPointerException("Cause")); 

     //throw exception 
     throw e; 
    } 

    public static void main(String args[]) { 
     try { 
      demo(); 
     } 
     catch(ArithmeticException e) { 
      //display top_level exception 
      System.out.println("Cautch : " + e); 
     } 

     //display cause exception 
     //getting error here 
     System.out.println("Main Cause : " + e.getCause()); 
    } 
} 

그래서 어떻게 원인 예외를 얻을 수 있습니까?

답변

0

변수 e 범위는 일예로 catch 블록 내부에

System.out.println("Main Cause : " + e.getCause()); 

이동 만 catch 블록까지 제한된다

try { 
     demo(); 
    } 
    catch(ArithmeticException e) { 
     //display top_level exception 
     System.out.println("Cautch : " + e); 
     System.out.println("Main Cause : " + e.getCause()); 
    } 
+0

아 ...... 그래서 실수가 ...... 알았어요 ..... –