当前位置:网站首页>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 .
边栏推荐
猜你喜欢

软件包设置成——>YUM源

SQL developer graphical window to create database (tablespace and user)

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

Complex floating point multiplication of vivado IP core floating point

Merkle tree existential function modified for the first time

5G服务化接口和参考点

Hongke education you want to enter the field of TSN? Hongke teaches you how to build TSN test system

Hongke | uses jesd204 serial interface to bridge analog and digital worlds at high speed

基于噪声伪标签和对抗性学习的医学图像分割注释有效学习

Hongke shares | how to test and verify complex FPGA designs (1) -- entity or block oriented simulation
随机推荐
Floating point addition and subtraction method of vivado IP core floating point
如何画出优秀的架构图
On defect description style
Online multiplayer chat room based on UDP communication
Annotation
OpenResty的核心与cosocket
比较单片机3种时钟电路方案
将源码包转换为rpm包
20个hacker神器
Arrays & object & System & Math & random & Packaging
5G服务化接口和参考点
一文看懂网络安全五年之巨变
解决文件大导致磁盘满的问题
Hongke case | PAC: an integrated control solution integrating SoftPLC control logic, HMI and other service functions
FIR filter design (1) -- using the FDATool toolbox of MATLAB to design FIR filter parameters
Hongke education you want to enter the field of TSN? Hongke teaches you how to build TSN test system
day15_ generic paradigm
成长为架构师途中的一些思考
Huawei switch ce12808 import and export configuration file
会话推荐中的价格偏好和兴趣偏好共同建模-论文泛读