当前位置:网站首页>Execution sequence of finally and return
Execution sequence of finally and return
2022-07-29 06:47:00 【The evil way does not miss the firewood cutting skill】
problem : stay try-catch-finally In the code ,return and finally Who will execute first ?
Code 1 :
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 The module is executed ");
}
}
The operation results are as follows :
finally The module is executed
1
From the above operation results , In carrying out the return Run before finally Code , Why is that so ? We can see the compiled code as follows :
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 The module is executed ");
}
return var1;
}
Found that after compiling return Optimized and put finally Back , Temporary variables are used in the middle var1 Stored , So naturally, the execution order is to output first finally And then execute return. meanwhile return It also means that this method is ready to exit after running .
Code 2 :
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 The code block is executed ");
return 10;
} finally {
System.out.println("finally The module is executed ");
}
}
The operation results are as follows :
catch The code block is executed
finally The module is executed
10
Why is this output , Let's look at the compiled code as follows :
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 The code block is executed ");
var2 = 10;
} finally {
System.out.println("finally The module is executed ");
}
return var2;
}
Or because the code is automatically optimized .
Code three :
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 The code block is executed ");
return 10;
} finally {
System.out.println("finally The module is executed ");
return 20;
}
}
}
The running results are as follows :
catch The code block is executed
finally The module is executed
20
After reading the running results, let's look at the compiled code as follows :
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 The code block is executed ");
var2 = true;
}
} finally {
System.out.println("finally The module is executed ");
return 20;
}
}
It can be seen from here that as long as finally Code terminator is added in return, Other places return All ignored , stay finally To add return It's equivalent to adding return The effect is the same , Look at the code below , Let it not run abnormally :
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 The code block is executed ");
return 10;
} finally {
System.out.println("finally The module is executed ");
return 20;
}
}
The operation results are as follows :
finally The module is executed
20
The compiled code looks like this :
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 The code block is executed ");
boolean var2 = true;
} finally {
System.out.println("finally The module is executed ");
return 20;
}
}
Find that as long as finally Add return, Finally, the return value of this method can only be finally Return value in .
边栏推荐
- Circular linked list and bidirectional linked list
- finally 和 return 的执行顺序
- 什么是DNS放大攻击
- 5、 Wireless communication network
- Hongke white paper | how to use TSN time sensitive network technology to build a digital factory in industry 4.0?
- 8、 Network security
- 为什么5G N2接口控制面使用SCTP协议?
- 解决分频模块modelsim下仿真输出为stx的错误
- The performance and viewing methods of websites attacked by DDoS
- 三、广域通信网
猜你喜欢

Hongke automation SoftPLC | Hongke kPa modk operation environment and construction steps (3) -- modk routine test

CNN-卷积神经网络

day12_多线程

Huawei switch ce12808 import and export configuration file

Hongke share | FPGA implementation of pass through and store and forward switching delay

etcd原理

day10_ Exception handling & enumeration

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

MQTT服务器搭建以及使用MQTT.fx测试

Hongke share | bring you a comprehensive understanding of "can bus error" (I) -- can bus error and error frame
随机推荐
Computer right mouse click always turn around what's going on
案例补充、ATM
JMM 内存模型概念
Complex floating point multiplication of vivado IP core floating point
After the EtherCAT master station is disconnected, how to ensure that the target system is not affected by the fault?
Traffic characteristics of webshell management tools
Leetcode question brushing record
Condition 条件对象源码浅读
Huawei switch ce12808 import and export configuration file
损失函数——交叉熵损失函数
day04_ array
20个hacker神器
4、 LAN and man
finally 和 return 的执行顺序
LDAP简述及统一认证说明
Hongke share | let you have a comprehensive understanding of "can bus errors" (IV) -- producing and recording can errors in practice
Design of IIR filter based on FPGA
Advanced socket programming (options and control information)
Multiple IO usage
Several misunderstandings about DDoS