請參閱以下代碼並解釋輸出行爲。返回語句在catch中的行爲,最後
public class MyFinalTest {
public int doMethod(){
try{
throw new Exception();
}
catch(Exception ex){
return 5;
}
finally{
return 10;
}
}
public static void main(String[] args) {
MyFinalTest testEx = new MyFinalTest();
int rVal = testEx.doMethod();
System.out.println("The return Val : "+rVal);
}
}
結果是返回瓦爾:10
Eclipse中顯示一個警告:finally block does not complete normally
。
catch塊中的return語句會發生什麼?
的可能重複[爲什麼改變返回的變量在finally塊不會改變返回值?](http://stackoverflow.com/questions/ 16030858 /爲什麼要改變返回變量在最後塊不改變回報) – fglez 2013-04-30 07:32:21
一個非常受歡迎的面試問題。 – 2014-06-29 20:09:12