《 Cracking the Coding Interview 》 - 제1 4 장: 자바 - 제목 2
3305 단어 interview
제목: 자바 의 try - catch - finally 구문 블록 에 catch 에 return 구문 이 있 으 면 finally 가 실 행 됩 니까?
해법
코드:
1 // 14.2 Will the code in finally {} be executed if there is a return statement inside try {} or catch{}?
2 // The answer is yes.
3 // OUTPUT:
4 // Hello world.
5 // An exception is caught: java.lang.ArrayIndexOutOfBoundsException: 2
6 // Finally you're here.
7 public class TestJava {
8 public static void main(String[] args) {
9 try {
10 System.out.println("Hello world.");
11 int[] a = new int[2];
12 a[2] = 1;
13 return;
14 } catch (Exception e) {
15 // TODO: handle exception
16 System.out.println("An exception is caught: " + e);
17 return;
18 } finally {
19 System.out.println("Finally you're here.");
20 }
21 }
22 }
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Js 연습 면접 질문우리 회사에서는 때때로 후보자 인터뷰에 참여하는데 주로 Node.js, AWS, 서버리스 스택에 관한 것입니다. 때때로 후보자의 기술을 평가하기 위해 코드의 출력에 대해 물어볼 수 있습니다. 내 경험상 이론을 잘 아...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.