当前位置:网站首页>finally 和 return 的执行顺序
finally 和 return 的执行顺序
2022-07-29 05:27:00 【魔道不误砍柴功】
问题:在 try-catch-finally 代码中,return 和 finally 谁先执行?
代码一:
public static void main(String[] args) {
int a = new ThreadPoolDemo().demo();
System.out.println(a);
}
public int demo() {
try {
return 1;
} finally {
System.out.println("finally模块被执行");
}
}
运行结果如下:
finally模块被执行
1
从上述运行结果来看,在执行到return之前先运行了finally代码,为什么会是这样的呢?我们可以看看编译后的代码如下:
public static void main(String[] args) {
int a = (new PersonDemo()).demo();
System.out.println(a);
}
public int demo() {
byte var1;
try {
var1 = 1;
} finally {
System.out.println("finally 模块被执行");
}
return var1;
}
发现编译后 return 被优化了放到了finally后面,中间都是用临时变量var1存储的,所以自然而然执行顺序就是先输出finally然后执行return。同时return也是表示这个方法运行完准备退出。
代码二:
public static void main(String[] args) {
int a = new ThreadPoolDemo().demo();
System.out.println(a);
}
public int demo() {
try {
int a = 1 / 0;
return 1;
} catch (Exception e) {
System.out.println("catch 代码块被执行");
return 10;
} finally {
System.out.println("finally 模块被执行");
}
}
运行结果如下:
catch 代码块被执行
finally 模块被执行
10
为什么是这样的输出结果,我们查看编译后的代码如下所示:
public static void main(String[] args) {
int a = (new PersonDemo()).demo();
System.out.println(a);
}
public int demo() {
byte var2;
try {
int a = 1 / 0;
var2 = 1;
return var2;
} catch (Exception var6) {
System.out.println("catch 代码块被执行");
var2 = 10;
} finally {
System.out.println("finally 模块被执行");
}
return var2;
}
还是因为代码自动优化了。
代码三:
public class PersonDemo {
public static void main(String[] args) {
int a = new PersonDemo().demo();
System.out.println(a);
}
public int demo() {
try {
int a = 1 / 0;
return 1;
} catch (Exception e) {
System.out.println("catch 代码块被执行");
return 10;
} finally {
System.out.println("finally 模块被执行");
return 20;
}
}
}
运行结果如下所示:
catch 代码块被执行
finally 模块被执行
20
看完运行结果我们看看编译后的代码如下所示:
public static void main(String[] args) {
int a = (new PersonDemo()).demo();
System.out.println(a);
}
public int demo() {
try {
boolean var2;
try {
int a = 1 / 0;
var2 = true;
} catch (Exception var6) {
System.out.println("catch 代码块被执行");
var2 = true;
}
} finally {
System.out.println("finally 模块被执行");
return 20;
}
}
从这里可以看出只要在finally中加了代码结束符return,其他地方的return都忽略,在finally中加return就相当于在代码最后一行加的return效果一样,看下面的代码,让它不跑异常:
public static void main(String[] args) {
int a = new PersonDemo().demo();
System.out.println(a);
}
public int demo() {
try {
return 1;
} catch (Exception e) {
System.out.println("catch 代码块被执行");
return 10;
} finally {
System.out.println("finally 模块被执行");
return 20;
}
}
运行结果如下:
finally 模块被执行
20
编译后的代码如下所示:
public static void main(String[] args) {
int a = (new PersonDemo()).demo();
System.out.println(a);
}
public int demo() {
try {
boolean var1 = true;
} catch (Exception var6) {
System.out.println("catch 代码块被执行");
boolean var2 = true;
} finally {
System.out.println("finally 模块被执行");
return 20;
}
}
发现只要在finally中加入return,最终这个方法的返回值只能是finally中的返回值。
边栏推荐
猜你喜欢
随机推荐
What if the 80443 port of the website server has been maliciously attacked?
Several misunderstandings about DDoS
Design of IIR filter based on FPGA
IPv6 representation and configuration cases
greenplum企业部署
5G服务化接口和参考点
Arrays&Object&System&Math&Random&包装类
day15_泛型
day03_ 2_ task
华为交换机CE12808导入导出配置文件
为什么5G N2接口控制面使用SCTP协议?
day16-集合上
vmstat 内存消耗查询
What is WAF protection
day10_ Exception handling & enumeration
网站被挂马的解决方案
MQTT服务器搭建以及使用MQTT.fx测试
Huawei switch ce12808 import and export configuration file
Is it OK to directly compare the size of two numbers in FPGA?
Day16 set









