2014년 1월 7일 화요일

[8-6] 코드 수행결과

class Exercise8_6 {
public static void main(String[] args) {
try {
method1 ();
}catch (Exception e) {
System.out.println(5);
}
}

static void method1 () {
try{
method2 ();
System.out.println(1);
} catch (ArithmeticException e) {
System.out.println(2);
} finally {
System.out.println(3);
}

System.out.println(4);
}

static void method2 () {
throw new NullPointerException ();
}
}

[결과]------------
3
5
------------------

[해설]--------------------------------
method2() 에서 'NullPointerException' 발생. try-catch 문이 없어서 메서드 종료.
이를 호출한 method1() 로 되돌아갔는데 여기도 'NullPointerException'을 처리해줄 블럭이 없으로므 메서드 종료.
이를 호출한 main 메서드로 돌아간다. 이 때 finally 블럭은 수행이 되므로 '3' 출력.
main 메서드에는 모든 예외를 처리할 Exception 이 선언된 catch 블럭이 있으므로 '5' 출력.
---------------------------------------

댓글 없음:

댓글 쓰기