当前位置:网站首页>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中的返回值。
边栏推荐
- 4、 LAN and man
- Hongke case | PAC: an integrated control solution integrating SoftPLC control logic, HMI and other service functions
- TCP socket communication experiment
- 用神经网络实现手写数字识别
- 基于FPGA的IIR型滤波器设计
- OpenResty的核心与cosocket
- How to pre circumvent the vulnerabilities of unsafe third-party components?
- Network Security Learning (I)
- day14_单元测试&日期常用类&字符串常用类
- day13_多线程下
猜你喜欢

Hongke white paper | how to use TSN time sensitive network technology to build a digital factory in industry 4.0?

Circular linked list and bidirectional linked list

注解(Annotation)

三、广域通信网

Merkle tree existential function modified for the first time

Design and simulation code of 4-bit subtracter based on FPGA

Arrays & object & System & Math & random & Packaging

Arrays&Object&System&Math&Random&包装类

day12_多线程

day14_ Unit test & Date common class & String common class
随机推荐
day06_类与对象
Hongke automation SoftPLC | modk operation environment and construction steps (1) -- Introduction to operation environment
day17_集合下
6、 Network interconnection and Internet
centos 部署postgresql 13
day03_2_作业
How to judge whether a business is attacked by DDoS? What harm will it cause?
Floating point multiplication and division of vivado IP core floating point
注解(Annotation)
day02_ Basic grammar
网站被挂马的解决方案
day15_ generic paradigm
day14_单元测试&日期常用类&字符串常用类
day10_ Exception handling & enumeration
Hongke case | PAC: an integrated control solution integrating SoftPLC control logic, HMI and other service functions
TCP socket communication experiment
find命令详解(文章最后运维最常用操作)
Hongke share | bring you a comprehensive understanding of "can bus error" (I) -- can bus error and error frame
Ultra low cost DDoS attacks are coming. See how WAF protects Jedi
OpenResty的核心与cosocket