转:Java中finally和return的执行关系

时间:2023-03-09 19:08:46
转:Java中finally和return的执行关系

finally可以分两方面理解

1.执行时机问题。finally总会执行(除非是System.exit()),正常情况下在try后执行,抛异常时在catche后面执行

2.返回值问题。可以认为try(或者catch)中的return语句的返回值放入线程栈的顶部:如果返回值是基本类型则顶部存放的就是值,如果返回值是引用类型,则顶部存放的是引用。finally中的return语句可以修改引用所对应的对象,无法修改基本类型。但不管是基本类型还是引用类型,都可以被finally返回的“具体值”具体值覆盖

3.不建议在finally中使用return语句,如果有,eclipse会warning“finally block does not complete normally”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.ljn.base;
/**
 * @author lijinnan
 * @date:2014-1-22
 */
public class FinallyTest {
    public static void main(String[] args) {
        System.out.println(test());
        System.out.println(testPrimitive());
    }
    public static Student test() {
        Student result = new Student("Tom", 0);
        try {
            result.setAge(1);
            return result;
        catch (Exception e) {
            result.setAge(2);
            return result;
        finally {
            result.setAge(3);       //引用类型的返回值,可被修改
            //return new Student("Kobe", 33);   //可以被“具体值”覆盖
        }
         
    }
     
    public static int testPrimitive() {
        int x = 0;
        try {
            x = 1;
            return x;
        catch (Exception e) {
            x = 2;
            return x;
        finally {
            x = 3;          //基本类型的返回值,不可被修改
            //return x;    //可以被“具体值”覆盖
        }
         
    }
     
    private static class Student {
        private String name;
        private int age;
         
        public Student(String name, int age) {
            this.name = name;
            this.age = age;
        }
        @Override
        public String toString() {
            return "Student [name=" + name + ", age=" + age + "]";
        }
        public void setAge(int age) {
            this.age = age;
        }
         
    }
}