当前位置:网站首页>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中的返回值。
边栏推荐
- Hongke automation SoftPLC | modk operation environment and construction steps (1) -- Introduction to operation environment
- Floating point addition and subtraction method of vivado IP core floating point
- 网站受DDoS攻击的表现以及查看方法
- Base64与File之间的相互转化
- 有用网站
- day17_ Under collection
- 如何在开发板上使用sftp命令访问sftp-server
- Joint use skills of joiner.on and stream().Map
- 失效的访问控制
- day15_ generic paradigm
猜你喜欢

Hongke case | PAC: an integrated control solution integrating SoftPLC control logic, HMI and other service functions

day04_ array

三、广域通信网

day16-集合上

Online multiplayer chat room based on UDP communication

day12_多线程

Merkletree builds QT implementation UI

LDAP简述及统一认证说明

day10_异常处理&枚举

Scanbasepackages scanning range configuration
随机推荐
Hongke will share the EtherCAT demo for you and teach you how to quickly transition from other protocols to EtherCAT industrial bus
Online multiplayer chat room based on UDP communication
What is DNS amplification attack
Solution for website being suspended
7、 Next generation Internet IPv6
Several misunderstandings about DDoS
成长为架构师途中的一些思考
pairs和ipairs的区别
失效的访问控制
After the EtherCAT master station is disconnected, how to ensure that the target system is not affected by the fault?
Hog+svm for pedestrian detection
将源码包转换为rpm包
Vivado IP核之复数浮点数乘法 Floating-point
How to pre circumvent the vulnerabilities of unsafe third-party components?
解决文件大导致磁盘满的问题
浅谈缺陷描写样式
day06_类与对象
Multithreaded server programming
day09_ Static & Final & code block & abstract class & Interface & internal class
day04_数组