Java中finally和return的关系实例解析

时间:2022-06-17 08:03:00

本文研究的主要是java中finallyreturn的关系,具体介绍和实例如下所示。

finally 和 return 关系的总结

1.try块中有system.exit(0)这样的语句,由于它是终止java虚拟机jvm的,连jvm都停止了,所有都结束了,当然finally语句也不会被执行到。
2.其它情况下,finally语句都必然会被执行。因此可以在这里执行一些资源的释放操作。

(1)finally中的return会覆盖try/catch中的renturn。
(2)在finally中写return语句会有警告,因为它会阻止函数抛出异常,而改为正常返回。

?
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package com.demo.test;
public class finallyandreturn {
    private static void finallyandtryreturn() {
        try {
            system.out.println("finallyandtryreturn -> try");
            return;
        }
        catch (exception e) {
            system.out.println("finallyandtryreturn -> catch");
        }
        finally {
            system.out.println("finallyandtryreturn -> finally");
        }
    }
    private static void finallyandcatchreturn() {
        try {
            system.out.println("finallyandcatchreturn -> try");
            throw new exception();
        }
        catch (exception e) {
            system.out.println("finallyandcatchreturn -> catch");
            return;
        }
        finally {
            system.out.println("finallyandcatchreturn -> finally");
        }
    }
    // finally语句是在try的return语句执行之后,return返回之前执行。
    private static string tryreturn() {
        string str = "initialized";
        try {
            system.out.println("tryreturn -> try");
            str = "try";
            return str;
        }
        catch (exception e) {
            system.out.println("tryreturn -> catch");
            str = "catch";
        }
        finally {
            system.out.println("tryreturn -> finally");
            str = "finally";
        }
        return null;
    }
    private static string tryreturnandfinallyreturn() {
        string str = "initialized";
        try {
            system.out.println("tryreturnandfinallyreturn -> try");
            str = "try";
            return str;
        }
        catch (exception e) {
            system.out.println("tryreturnandfinallyreturn -> catch");
            str = "catch";
        }
        finally {
            system.out.println("tryreturnandfinallyreturn -> finally");
            /*
       * warning: finally block does not complete normally
       * 如果finally块中包含了return语句,即使前面的catch块重新抛出了异常,则调用该方法的语句也不会获得catch块重新抛出的异常,而是会得到finally块的返回值,并且不会捕获异常。
       */
            str = "finally";
            return str;
        }
    }
    private static string trythrowandfinallyreturn() throws exception {
        string str = "initialized";
        try {
            system.out.println("trythrowandfinallyreturn -> try");
            str = "try";
            throw new exception();
        }
        catch (exception e) {
            system.out.println("trythrowandfinallyreturn -> catch");
            str = "catch";
            throw new exception();
        }
        finally {
            system.out.println("trythrowandfinallyreturn -> finally");
            /*
       * warning: finally block does not complete normally
       * 如果finally块中包含了return语句,即使前面的catch块重新抛出了异常,则调用该方法的语句也不会获得catch块重新抛出的异常,而是会得到finally块的返回值,并且不会捕获异常。
       */
            str = "finally";
            return str;
        }
    }
    private static void finallyandruntimeexception() {
        try {
            system.out.println("finallyandruntimeexception -> try");
            throw new runtimeexception();
        }
        catch (exception e) {
            system.out.println("finallyandruntimeexception -> catch");
        }
        finally {
            system.out.println("finallyandruntimeexception -> finally");
        }
    }
    private static void finallyandexit() {
        try {
            system.out.println("finallyandexit -> try");
            // system.exit(0);是终止java虚拟机jvm的,连jvm都停止了,所有都结束了,当然finally语句也不会被执行到。
            system.exit(0);
        }
        catch (exception e) {
            system.out.println("finallyandexit -> catch");
        }
        finally {
            system.out.println("finallyandexit -> finally");
        }
    }
    public static void main(string[] args) {
        finallyandtryreturn();
        system.out.println();
        finallyandcatchreturn();
        system.out.println();
        system.out.println("tryreturn return -> " + tryreturn());
        system.out.println();
        system.out.println("tryreturnandfinallyreturn return -> " + tryreturnandfinallyreturn());
        system.out.println();
        try {
            system.out.println("trythrowandfinallyreturn return -> " + trythrowandfinallyreturn());
        }
        catch (exception e) {
            e.printstacktrace();
        }
        system.out.println();
        finallyandruntimeexception();
        system.out.println();
        finallyandexit();
    }
}

演示结果:

Java中finally和return的关系实例解析

总结

以上就是本文关于java中finally和return的关系实例解析的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

原文链接:http://blog.csdn.net/chy555chy/article/details/52791338