문제
다음 Java 코드의 실행 결과를 쓰시오.
Javapublic class Main { static String execute() { try { System.out.print("A"); throw new RuntimeException(); } catch (RuntimeException ex) { System.out.print("B"); return "X"; } finally { System.out.print("C"); } } public static void main(String[] args) { System.out.print(execute()); } }
정답
ABCX
ABCX
해설
try 블록에서 "A"가 출력되고 RuntimeException이 발생합니다. catch 블록에서 예외를 처리하여 "B"가 출력되고 "X"를 반환하려 하지만, finally 블록이 먼저 실행되어 "C"가 출력됩니다. 그 후 catch 블록의 반환값 "X"가 main에서 출력되어 최종적으로 "ABCX"가 출력됩니다.