- try中有return:先執(zhí)行try代碼塊,return語句或者函數(shù)也先執(zhí)行,然后執(zhí)行finally語句,最后執(zhí)行try中的return返回結(jié)果。
public class FinallyTest1 {
public static void main(String[] args) {
System.out.println(test11());
}
public static String test11() {
try {
System.out.println("try block");
return test12();
} finally {
System.out.println("finally block");
}
}
public static String test12() {
System.out.println("return statement");
return "after return";
}
}
結(jié)果:
try block
return statement
finally block
after return
- try中和finally中都有return,finally中return會覆蓋try中return
public class FinallyTest2 {
public static void main(String[] args) {
System.out.println(test2());
}
public static int test2() {
int b = 20;
try {
System.out.println("try block");
return b += 80;
} catch (Exception e) {
System.out.println("catch block");
} finally {
System.out.println("finally block");
if (b > 25) {
System.out.println("b>25, b = " + b);
}
return 200;
}
}
}
結(jié)果:
try block
finally block
b>25, b = 100
200
- 如果finally中沒有return語句,只是修改返回值的語句,那么結(jié)果可能被修改也可能不會。如果try中return簡單的值類型,finally中修改無效,如果try中return一個對象,finally中可以操作該對象。
public class FinallyTest3 {
public static void main(String[] args) {
System.out.println(test3());
}
public static int test3() {
int b = 20;
try {
System.out.println("try block");
return b += 80;
} catch (Exception e) {
System.out.println("catch block");
} finally {
System.out.println("finally block");
if (b > 25) {
System.out.println("b>25, b = " + b);
}
b = 150;
}
return 2000;
}
}
結(jié)果:
try block
finally block
b>25, b = 100
100
public class FinallyTest6
{
public static void main(String[] args) {
System.out.println(getMap().get("KEY").toString());
}
public static Map<String, String> getMap() {
Map<String, String> map = new HashMap<String, String>();
map.put("KEY", "INIT");
try {
map.put("KEY", "TRY");
return map;
}
catch (Exception e) {
map.put("KEY", "CATCH");
}
finally {
map.put("KEY", "FINALLY");
map = null;
}
return map;
}
}
結(jié)果:
FINALLY
- try中發(fā)送異常的時候,try中的return語句不會執(zhí)行,但是return行為會最后執(zhí)行,catch和finally會執(zhí)行并且會改變結(jié)果,因為被異常捕獲,try-catch-finnaly塊外面的return永遠不會被執(zhí)行。
public class FinallyTest4 {
public static void main(String[] args) {
System.out.println(test4());
}
public static int test4() {
int b = 20;
try {
System.out.println("try block");
b = b / 0;
return b += 80;
} catch (Exception e) {
b += 15;
System.out.println("catch block");
} finally {
System.out.println("finally block");
if (b > 25) {
System.out.println("b>25, b = " + b);
}
b += 50;
}
return 204;
}
}
結(jié)果:
try block
catch block
finally block
b>25, b = 35
85
- catch中如果有return,情況跟try中的return一致
public class FinallyTest5 {
public static void main(String[] args) {
System.out.println(test5());
}
public static int test5() {
int b = 20;
try {
System.out.println("try block");
b = b /0;
return b += 80;
} catch (Exception e) {
System.out.println("catch block");
return b += 15;
} finally {
System.out.println("finally block");
if (b > 25) {
System.out.println("b>25, b = " + b);
}
b += 50;
}
}
}
結(jié)果:
try block
catch block
finally block
b>25, b = 35
35