public class FinallyReturnTest {
public static void main(String[] args) {
System.out.print("value = " + testMethod());
}
public static int testMethod() {
try {
System.out.println("before exception");
int y = 1;
int x = y/0;
System.out.println("after exception");
} catch(Exception e) {
System.out.println("catcher");
return 1;
} finally {
System.out.println("finally");
// return 2;
}
// System.out.println("outside");
return 3;
}
}
运行结果是:
before exception
catcher
finally
value = 1
catch中有return,finally中没有return,通过debug代码可知,先执行finally中的代码,然后再执行catcher中return 1。 |