문제
다음 Java 코드의 실행 결과를 쓰시오.
Javapublic class Main { static String test() { try { throw new IllegalArgumentException(); } catch (IllegalArgumentException e) { return "A"; } finally { System.out.print("B"); } } public static void main(String[] args) { System.out.print(test()); } }
정답
BA
BA
해설
try 블록에서 IllegalArgumentException이 발생하여 catch 블록이 실행되어 "A"를 반환하기로 결정됩니다. 하지만 finally 블록이 먼저 실행되어 "B"를 출력한 후, catch 블록의 반환값 "A"가 출력되어 "BA"가 됩니다.