1.return与finally到底谁先执行?
我们经常会使用try/catch/finally语句块。当然,return关键字使用也是很平常的事,但是不知道大家有没有注意个这样一个问题。
当在try语句块里面使用return语句,在finally里面去修改return所要返回的内容会出现什么情况。
首先,我们知道return是结束方法的标志,一旦方法执行到return语句就将返回不再往下执行。
其次,我们也知道,finally里面的语句是无论方法怎样执行,最后都要执行finally里面的语句。
那么究竟是先执行return还是finally呢?下面通过两个小实验来解决这个问题。
首先看第一个例子:
public class TestTryCatch {
public static void main(String[] args)
{
TestTryCatch test = new TestTryCatch();
System.out.println(test.fun());
}
public int fun()
{
int i = 10;
try
{
//doing something
return i;
}catch(Exception e){
return i;
}finally{
i = 20;
}
}
}
输出结果:10
很简单的一个例子,创建了一个方法fun,在方法里使用try/catch语句,方法要求返回值类型为int型。在try里面放回i,这个时候是10,但是在finally里面将i值修改为20。我们看到结果是10,好像是return先执行。那么接下来再看另一个例子:
public class TestTryCatch {
public static void main(String[] args)
{
TestTryCatch test = new TestTryCatch();
System.out.println(test.fun());
}
public StringBuilder fun()
{
StringBuilder s = new StringBuilder("Hello");
try
{
//doing something
s.append("Word");
return s;
}catch(Exception e){
return s;
}finally{
string.append("finally");
}
}
}
输出结果:HelloWordFinally
看结果似乎有点出乎意料了,因为这次finally里面修改的内容生效了。看代码其实差别不大,只是把返回值类型修改为StringBuilder了。那么这是为什么呢?下面就为大家解释一下其中到底是怎么执行的。
首先,拿第一个例子来说,可以在main方法里实现这样一条语句:
int result = test.fun();我们知道这样做是没有问题的,但是大家都知道“=”号赋值是常量赋值。
但是,方法的存放地址和常量的存放地址是不一样的,方法的存放在代码区的。
上面我们把一个方法赋值给一个int型也没有报错。那是因为在声明方法是我们声明了返回值类型。那么编译器就会在代码的最前端预留一段返回值类型的内存。执行return的时候,就会把返回的内容写入到这段内存中。
这样,执行“=”号赋值的时候,就能在内存中匹配到相同的类型。赋值便能成功。
弄清楚上面的道理之后,再来解释最开始提出的问题就容易多了。在执行了return之后,返回的值已经被写入到那段内存中了,finally再修改i的值,只是修改了后面代码段的i值,对返回段内存没有影响。至于第二个例子,再看下面这张图你就会明白。
我们可以看到,当返回值不是基本数据类型的时候,其是指向一段内存的,return将返回段指向一段内存,但是代码段的s依然是指向的同一段内存地址,所以当s修改它指向内存中的值的时候,其实也就修改了返回段指向内存中的值,所以最终的值改变了。
到底返回值变不变可以简单的这么记忆:当finally调用的任何可变API,会修改返回值;当finally调用任何的不可变API,对返回值没有影响。
总结一下:return与finally并没有明显的谁强谁弱。
在执行时,是return语句先把返回值写入内存中,然后停下来等待finally语句块执行完,return再执行后面的一段。
2.JAVA异常处理之finally中最好不要使用return
finally 语句块中, 最好不要使用return, 否则会造成已下后果;
1, 如果catch块中捕获了异常, 并且在catch块中将该异常throw给上级调用者进行处理, 但finally中return了, 那么catch块中的throw就失效了, 上级方法调用者是捕获不到异常的. 见demo如下:
1 public class TestException {
2 public TestException() {
3 }
4
5 boolean testEx() throws Exception {
6 boolean ret = true;
7 try {
8 ret = testEx1();
9 } catch (Exception e) {
10 System.out.println("testEx, catch exception");
11 ret = false;
12 throw e;
13 } finally {
14 System.out.println("testEx, finally; return value=" + ret);
15 return ret;
16 }
17 }
18
19 boolean testEx1() throws Exception {
20 boolean ret = true;
21 try {
22 ret = testEx2();
23 if (!ret) {
24 return false;
25 }
26 System.out.println("testEx1, at the end of try");
27 return ret;
28 } catch (Exception e) {
29 System.out.println("testEx1, catch exception");
30 ret = false;
31 throw e;
32 } finally {
33 System.out.println("testEx1, finally; return value=" + ret);
34 return ret;
35 }
36 }
37
38 boolean testEx2() throws Exception {
39 boolean ret = true;
40 try {
41 int b = 12;
42 int c;
43 for (int i = 2; i >= -2; i--) {
44 c = b / i;
45 System.out.println("i=" + i);
46 }
47 return true;
48 } catch (Exception e) {
49 System.out.println("testEx2, catch exception");
50 ret = false;
51 throw e;
52 } finally {
53 System.out.println("testEx2, finally; return value=" + ret);
54 return ret;
55 }
56 }
57
58 public static void main(String[] args) {
59 TestException testException1 = new TestException();
60 try {
61 testException1.testEx();
62 } catch (Exception e) {
63 e.printStackTrace();
64 }
65 }
66 }
该方法的最终执行结果如下:
i=2
i=1
testEx2, catch exception
testEx2, finally; return value=false
testEx1, finally; return value=false
testEx, finally; return value=false
尽管testEx2中的catch抛出异常, 但因为finally中return了, 那么testEx1中是捕获不到异常的, 所以testEx1中的catch块是不会执行的。
不管是给try块中造了个异常,还是在try块中进行return,我们发现finally块还是会执行的。因为异常处理设计初衷就是让finally块始终执行。
三、finally执行时机探讨
首先看常规情况:
public class TryCatchTest {
private static int total() {
try {
System.out.println("执行try");
return 11;
} finally {
System.out.println("执行finally");
}
}
public static void main(String[] args) {
System.out.println("执行main:" + total());
}
}
输出结果:
执行try
执行finally
执行main:11
分析一下,不难得出在这个例子中finally块执行在try块的return之前。我们给try块中造一个异常:
public class TryCatchTest {
private static int total() {
try {
System.out.println("执行try");
return 1 / 0;
} catch (Exception e) {
System.out.println("执行catch");
return 11;
} finally {
System.out.println("执行finally");
}
}
public static void main(String[] args) {
System.out.println("执行main:" + total());
}
}
输出结果:
执行try
执行catch
执行finally
执行main:11
同样的,finally执行在catch块return的执行前。