首先對按鈕添加點擊事件,事件代碼爲:System.out.println(test());
一、無異常時
1、
private String test(){
try {
System.out.println("try...");
} catch (Exception e) {
e.getMessage();
System.out.println("catch...");
} finally {
System.out.println("finally...");
}
return "return at last...";
}
運行結果:
2019-07-15 10:21:52.049 22618-22618/com.example.mymvp I/System.out: try...
2019-07-15 10:21:52.049 22618-22618/com.example.mymvp I/System.out: finally...
2019-07-15 10:21:52.049 22618-22618/com.example.mymvp I/System.out: return at last...
2、
private String test(){
try {
System.out.println("try...");
return "return at try...";
} catch (Exception e) {
e.getMessage();
System.out.println("catch...");
} finally {
System.out.println("finally...");
}
return "return at last...";
}
運行結果:
2019-07-15 10:25:13.605 22816-22816/com.example.mymvp I/System.out: try...
2019-07-15 10:25:13.605 22816-22816/com.example.mymvp I/System.out: finally...
2019-07-15 10:25:13.605 22816-22816/com.example.mymvp I/System.out: return at try...
3、
private String test(){
try {
System.out.println("try...");
return "return at try...";
} catch (Exception e) {
e.getMessage();
System.out.println("catch...");
} finally {
System.out.println("finally...");
return "return at finally...";
}
}
運行結果:
2019-07-15 10:26:35.815 23033-23033/com.example.mymvp I/System.out: try...
2019-07-15 10:26:35.815 23033-23033/com.example.mymvp I/System.out: finally...
2019-07-15 10:26:35.815 23033-23033/com.example.mymvp I/System.out: return at finally...
結論:無異常時,首先執行try代碼塊,如果try代碼塊有return則在執行return語句前跳轉到finally代碼塊執行,如果無return,則執行完整個try代碼塊後跳轉到finally代碼塊,執行finally代碼塊,如果finally代碼塊有return,則執行return返回,如果沒有則執行完finally代碼塊後回到try的return語句處執行並返回。
二、有異常時
1、
private String test(){
try {
System.out.println("try...");
System.out.println("exception before...");
int i = 1/0;
System.out.println("exception after...");
} catch (Exception e) {
e.getMessage();
System.out.println("catch...");
} finally {
System.out.println("finally...");
}
return "return at last...";
}
執行結果
2019-07-15 10:29:27.109 23259-23259/com.example.mymvp I/System.out: try...
2019-07-15 10:29:27.109 23259-23259/com.example.mymvp I/System.out: exception before...
2019-07-15 10:29:27.110 23259-23259/com.example.mymvp I/System.out: catch...
2019-07-15 10:29:27.110 23259-23259/com.example.mymvp I/System.out: finally...
2019-07-15 10:29:27.110 23259-23259/com.example.mymvp I/System.out: return at last...
2、
private String test(){
try {
System.out.println("try...");
System.out.println("exception before...");
int i = 1/0;
System.out.println("exception after...");
return "return at try...";
} catch (Exception e) {
e.getMessage();
System.out.println("catch...");
return "return at catch...";
} finally {
System.out.println("finally...");
}
}
執行結果
2019-07-15 10:31:12.413 23600-23600/com.example.mymvp I/System.out: try...
2019-07-15 10:31:12.413 23600-23600/com.example.mymvp I/System.out: exception before...
2019-07-15 10:31:12.413 23600-23600/com.example.mymvp I/System.out: catch...
2019-07-15 10:31:12.413 23600-23600/com.example.mymvp I/System.out: finally...
2019-07-15 10:31:12.413 23600-23600/com.example.mymvp I/System.out: return at catch...
3、
private String test(){
try {
System.out.println("try...");
System.out.println("exception before...");
int i = 1/0;
System.out.println("exception after...");
return "return at try...";
} catch (Exception e) {
e.getMessage();
System.out.println("catch...");
return "return at catch...";
} finally {
System.out.println("finally...");
return "return at finally...";
}
}
執行結果
2019-07-15 10:32:32.494 23784-23784/com.example.mymvp I/System.out: try...
2019-07-15 10:32:32.494 23784-23784/com.example.mymvp I/System.out: exception before...
2019-07-15 10:32:32.494 23784-23784/com.example.mymvp I/System.out: catch...
2019-07-15 10:32:32.494 23784-23784/com.example.mymvp I/System.out: finally...
2019-07-15 10:32:32.494 23784-23784/com.example.mymvp I/System.out: return at finally...
4、
private String test(){
try {
System.out.println("try...");
System.out.println("exception before...");
int i = 1/0;
System.out.println("exception after...");
return "return at try...";
} catch (Exception e) {
e.getMessage();
System.out.println("catch...");
} finally {
System.out.println("finally...");
}
return "return at last...";
}
結果:
2019-07-22 15:18:17.325 7549-7549/com.example.mymvp I/System.out: try...
2019-07-22 15:18:17.325 7549-7549/com.example.mymvp I/System.out: exception before...
2019-07-22 15:18:17.326 7549-7549/com.example.mymvp I/System.out: catch...
2019-07-22 15:18:17.326 7549-7549/com.example.mymvp I/System.out: finally...
2019-07-22 15:18:17.326 7549-7549/com.example.mymvp I/System.out: return at last...
如果catch和finally中沒有return,而在try發生異常的後面有return時,try中的return並不執行,切記。
總結:如果有異常時,首先執行try代碼塊到發生異常的地方,然後執行catch代碼塊到return語句的前一行代碼處(如果return存在),然後再執行finally代碼塊,如果finally中有return,則執行並返回,如果沒有,再檢查catch中是否有return,有則執行並返回,無則跳出try catch finally代碼塊。
總結:
整個try catch finally 與 return 結合使用的執行,首先按照try catch finally的順序正常執行代碼,這個過程中不執行return(如果存在return的話) ,執行完成後,再反向finaly catch try 的順序去查找return,執行遇到的第一個return,然後返回,代碼執行完成。
但是,依然有一個需要特別注意的地方,如下:
1、
public static String test() {
String str = "A";
try {
str = "B";
return "the return is: " + str;
} finally {
System.out.println("finally change return string to C");
str = "C";
//return "the return is" + str;
}
}
執行結果:
finally change return string to C
the return is: B
表示finally中的代碼str = "C";並沒有生效。
如果:
2、
public static String test() {
String str = "A";
try {
str = "B";
//return "the return is: " + str;
} finally {
System.out.println("finally change return string to C");
str = "C";
return "the return is: " + str;
}
}
執行結果:
finally change return string to C
the return is: C
表示finally中的代碼修改str = "C";生效了
表示,如果finally中修改某變量,如果return在finally中,則該修改生效,如果return在try中,則該修改並不生效,需要特別注意。