2011-03-16 2 views

답변

14

네, 유일한 예외가 네 마침내 당신이 return

public static void foo() { 
     try { 
      return; 
     } finally { 
      System.out.println("Finally.."); 
     } 
    } 

    public static void main(String[] args) { 
     foo(); 
    } 

가 출력 경우에도 실행 얻을 것이다

+3

당신이 일반적으로 원하지 않는 어떤 것 :-) –

4

try 블록에서 System.exit(1)입니다 것입니다 :

Finally.. 
0

네, 마지막으로 실행됩니다 return 성명 뒤에 있지만.

+1

참고 : http : //stackoverflow.com/questions/2824746/finally-and-return –

0

예. finally 문은 실행이 try 문으로 넘어 가면 어떤 경우에도 실행되도록 설계되었습니다.

1

return 문이 관련 try 블록 앞에있는 경우에는 그렇지 않습니다.

반환 문이 연결된 try 블록 안에 있으면 예. 우리가 System.exit(int) 또는 Runtime.getRuntime().exit(int)를 호출하여 JVM을 종료 할 때

public void foo(int x) 
{ 
    if (x < 0) 
     return; // finally block won't be executed for negative values 

    System.out.println("This message is never printed for negative input values"); 
    try 
    { 
     System.out.println("Do something here"); 
     return;  
    } 
    finally 
    { 
     System.out.println("This message is always printed as long as input is >= 0"); 
    } 
} 
+0

[마지막으로]는 코드가 ' 시도 '블록. –

0

finally 블록은 실패합니다.

+0

'Runtime.halt()', 너무 :-) –

0

다른 답변 외에, finally 블록에 return이 있으면 반환 이후의 명령문은 실행되지 않습니다.

finally 
    { 
     System.out.println("In finally"); 
     if (1 == 1) 
      return; 
     System.out.println("Won't get printed.); 
    } 

위의 스 니펫에서 "In finally to"는 표시되지만 "Not will get print"는 표시되지 않습니다.

관련 문제