문제
다음 Java 코드의 실행 결과를 쓰시오.
Javapublic class Main { static String execute() { try { throw new RuntimeException(); } catch (RuntimeException ex) { System.out.print("C"); return "Done"; } finally { System.out.print("End"); } } public static void main(String[] args) { String result = execute(); System.out.print(result); } }
정답
CEndDone
CEndDone
해설
RuntimeException이 발생하여 catch 블록이 실행되어 "C"를 출력합니다. catch에서 return "Done"이 결정되지만, finally 블록이 먼저 실행되어 "End"를 출력합니다. 그 후 "Done"이 반환되어 main에서 출력됩니다. 최종 결과는 "CEndDone"입니다.