문제
다음 Java 프로그램의 실행 결과는?
public class Test { public static void main(String[] args) { try { String str = null; System.out.print(str.length()); } catch (NullPointerException e) { System.out.print("NULL "); } catch (RuntimeException e) { System.out.print("RUNTIME "); } finally { System.out.print("END"); } } }
① NULL END ② RUNTIME END ③ NULL RUNTIME END ④ END
정답
1번
해설
null.length() 호출 시 NullPointerException이 발생한다. NullPointerException은 RuntimeException의 하위 클래스이므로 첫 번째 catch 블록에서 잡혀 'NULL '이 출력된다. 두 번째 catch는 실행되지 않는다. finally 블록은 항상 실행되어 'END'가 출력된다.