加载中...
加载中...
try..catch里面有return语句,finally里面的语句还会执行吗?

try..catch里面有return语句,finally里面的语句还会执行吗? 原创

try..catch里面有return语句,finally里面的语句还会执行吗?
1、不管有没有异常,finally中的代码都会执行 try执行完成之后,finally是一定会执行的
2、不发生异常的情况
    2.1、如果finally中有return语句,则会将try中的return语句”覆盖“掉,直接执行finally中的return语句,得到返回值,这样便无法得到try之前保留好的返回值。
    2.2、如果finally中没有return语句,也没有改变要返回值,则执行完finally中的语句后,会接着执行try中的return语句,返回之前保留的值。
    2.3、如果finally中没有return语句,但是改变了要返回的值,这里有点类似与引用传递和值传递的区别,分以下两种情况,:
        2.3.1 如果return的数据是基本数据类型或文本字符串,则在finally中对该基本数据的改变不起作用,try中的return语句依然会返回进入finally块之前保留的值。
        2.3.2 如果return的数据是引用数据类型,而在finally中对该引用数据类型的属性值的改变起作用,try中的return语句返回的就是在finally中改变后的该属性的值。
 3、发生异常
     3.1 如果finally中改变了要返回的值返回,返回的都是改变后的值。无论还基本类型还是引用类型。除了catch中有return的情况,返回的是catch中的值。

复制收展Javapackage com.algorithm.demo;

/**
* @Desc 累行客
* try..catch里面有return语句,finally里面的语句还会执行吗?
* 1、不管有没有异常,finally中的代码都会执行 try执行完成之后,finally是一定会执行的
*
* 2、不发生异常的情况
* 2.1、如果finally中有return语句,则会将try中的return语句”覆盖“掉,直接执行finally中的return语句,得到返回值,这样便无法得到try之前保留好的返回值。
* 2.2、如果finally中没有return语句,也没有改变要返回值,则执行完finally中的语句后,会接着执行try中的return语句,返回之前保留的值。
* 2.3、如果finally中没有return语句,但是改变了要返回的值,这里有点类似与引用传递和值传递的区别,分以下两种情况,:
* 2.3.1 如果return的数据是基本数据类型或文本字符串,则在finally中对该基本数据的改变不起作用,try中的return语句依然会返回进入finally块之前保留的值。
* 2.3.2 如果return的数据是引用数据类型,而在finally中对该引用数据类型的属性值的改变起作用,try中的return语句返回的就是在finally中改变后的该属性的值。
*
* 3、发生异常
* 3.1 如果finally中改变了要返回的值返回,返回的都是改变后的值。无论还基本类型还是引用类型。除了catch中有return的情况,返回的是catch中的值。
*
* @Author luolei
* @Web http://www.leixingke.com/
* @Date 2020/11/06 18:42
*/
public class ReturnInTryCatch {

public static void main(String[] args) {
System.out.println("-------test1---------");
System.out.println("return "+test1());
System.out.println("-------test11---------");
System.out.println("return "+test11());
System.out.println("-------test12---------");
System.out.println("return "+test12().toString());
System.out.println("-------test13---------");
System.out.println("return "+test13());
System.out.println("-------test2---------");
System.out.println("return "+test2());
System.out.println("-------test3---------");
System.out.println("return "+test3());
System.out.println("-------test4---------");
System.out.println("return "+test4());
}
/**
* @Desc: try{return} 没有异常
* 结果
* try
* finally
* return 20
* 没有异常,如果在try中return的情况下,先把try中将要return的值先存到一个本地变量中,即本例中的a=20将会被保存下来。接下来去执行finally语句,最后返回的是存在本地变量中的值,即返回a=20.
* @return: int
*/
public static int test1(){
int a=10;
try {
System.out.println("try");
a=20;
return a;
}finally {
a=40;
System.out.println("finally");
}
}

/**
* @Desc: try{return} 有异常
* 结果
* try
* catch
* finally
* ...
* return 40
* 有异常,如果在try中return的情况下,接下来去执行finally语句,最后返回的是在finally被改变的值,即返回a=40.
*
* @return: int
*/
public static int test11(){
int a=10;
try {
System.out.println("try");
System.out.println("1/0="+1/0);
a=20;
System.out.println("try after");
return a;
}catch (ArithmeticException e){
a=30;
System.out.println("catch");
}finally {
a=40;
System.out.println("finally");
}
System.out.println("...");
return a;
}

static class Student{
String name;
int age;

@Override
public String toString() {
return name+"-"+age;
}
}
/**
* @Desc: try{return} 没有异常 引用对象
* 结果
* try
* finally
* return 40-40
* 没有异常,如果在try中return的情况下,接下来去执行finally语句,注意:如果改变的是引用对象的值,最后返回的是在finally被改变的值。
* @return: int
*/
public static Student test12(){
Student s= new Student();
try {
System.out.println("try");
s.name="20";
s.age=20;
return s;
}finally {
s.name="40";
s.age=40;
System.out.println("finally");
}
}

/**
* @Desc: try{return} 有异常 引用对象
* 结果
* try
* catch
* finally
* ...
* return 40-40
*
* @return: int
*/
public static Student test13(){
Student s= new Student();
try {
System.out.println("try");
System.out.println("1/0="+1/0);
s.name="20";
s.age=20;
return s;
}catch (ArithmeticException e){
s.name="30";
s.age=30;
System.out.println("catch");
}finally {
s.name="40";
s.age=40;
System.out.println("finally");
}
System.out.println("...");
return s;
}

/**
* @Desc: catch(){return}
* 结果
* try
* catch
* finally
* return 30
* @return: int
*/
public static int test2(){
int a=10;
try {
System.out.println("try");
System.out.println("1/0="+1/0);
a=20;
}catch (ArithmeticException e){
a=30;
System.out.println("catch");
return a;
}finally {
a=40;
System.out.println("finally");
}
System.out.println("...");
return a;
}

/**
* @Desc: finally{return}
* 结果
* try
* catch
* finally
* return 40
* 如果在finally里也用了return语句。那么程序返回值会是40。因为规范规定了,当try和finally里都有return时,会忽略try的return,而使用finally的return。
* @return: int
*/
public static int test3(){
int a=10;
try {
System.out.println("try");
System.out.println("1/0="+1/0);
a=20;
}catch (ArithmeticException e){
a=30;
System.out.println("catch");
}finally {
a=40;
System.out.println("finally");
return a;
}
}


/**
* @Desc: finally{return}
* 结果
* try
* finally
* return 40
* 如果在finally里也用了return语句。那么程序返回值会是40。因为规范规定了,当try和finally里都有return时,会忽略try的return,而使用finally的return。
* @return: int
*/
public static int test4(){
int a=10;
try {
System.out.println("try");
a=20;
return a;
}catch (ArithmeticException e){
a=30;
System.out.println("catch");
return a;
}finally {
a=40;
System.out.println("finally");
return a;
}
}

}
  • 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
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236


没有更多推荐了 [去首页]
image
文章
376
原创
293
转载
83
翻译
0
访问量
183398
喜欢
73
粉丝
5
码龄
7年
资源
3

文章目录

加载中...
0
0