当前位置:网站首页>Example of try catch finally execution sequence
Example of try catch finally execution sequence
2022-06-13 03:52:00 【Brother Yu...】
public class TryDemo {
public static void main(String[] args) {
System.out.println(test());
}
public static int test() {
try {
return 1;
} catch (Exception e) {
return 2;
} finally {
System.out.print("3");
}
}
}
Program first enters try in , return 1, Then go back to continue finally Print 3, The function is finished , take 1 Return to the main function , Then the main function outputs 1
public static void main(String[] args) {
System.out.println(test1());
}
public static int test1() {
try {
return 2;
} finally {
return 3;
}
}
In principle, it will return first 2, Then enter finally Back in 3, But a program can only have one return value ,finally The block is the last to execute , So the return is 3, The end result is 3
public static void main(String[] args) {
System.out.println(test1());
}
public static int test1() {
int i = 0;
try {
i = 2;
return i;
} finally {
i = 3;
}
}
Get into try Give first i The assignment is 2, Then return it to , Next close finally Block medium i Set to 3,finally The code in is just for i Assigned value , Does not change the return value i Value , So the return is 2, The end result is i
Conclusion :
1、 No matter if there is any abnormality ,finally All the code in the block will execute ;
2、 When try and catch There is return when ,finally Still execute ;
3、finally Is in return The following expression is evaluated ( The calculated value is not returned , Instead, save the value you want to return , No matter finally What about the code in , The value returned will not change , It's still the value that we saved before ), So the return value of the function is at finally Determined before execution ;
4、finally It is best not to include return, Otherwise the program will exit early , The return value is not try or catch The return value saved in
边栏推荐
- Among the four common technologies for UAV obstacle avoidance, why does Dajiang prefer binocular vision
- 单片机:红外遥控通信原理
- 单片机:D/A 输出
- 单片机:A/D 和 D/A 的基本概念
- [test development] automated test selenium (II) -- common APIs for webdriver
- [test development] automated test selenium (III) -- unittest framework analysis
- Spark Optimization -- differences and policy selection of RDD cache (cache, persist, checkpoint)
- Mobile communication exercises
- Oracle database management
- 19 MySQL database optimization methods
猜你喜欢
机器人避障系统基础
单片机:Modbus 多机通信程序设计
LVS四层负载均衡集群(6)LVS工作模式
[test development] advanced part - Classification of various test technologies
[test development] installation of test management tool Zen path
Express 100
[test development] file compression project practice
[test development] blog system - LoadRunner performance test (publish blog function benchmark test)
单片机:I2C通信协议讲解
EGO planner论文翻译
随机推荐
Wechat payment configuration
Byte stream & buffer stream
单片机:A/D 和 D/A 的基本概念
[test development] installation of test management tool Zen path
【测试开发】文件压缩项目实战
5G China unicom 直放站 网管协议 实时性要求
大五人格学习记录
Precautions for stream flow
Field * doesn't have a default value problem
LVS 4 - tier Load Balancing Cluster (3) Cluster Function Classification - HPC
Flex layout
[Yugong series] June 2022 Net architecture class 080 master cluster and database switching of distributed middleware schedulemaster
单片机:A/D(模数转换)的主要指标
Binocular vision -- creating an "optimal solution" for outdoor obstacle avoidance
Lambda end operation find and match findany
Oracle built-in functions
【測試開發】自動化測試selenium(三)——unittest框架解析
MySQL 8.0 enables remote root user access and solves the problem of you are not allowed to create a user with Grant
【面试复习】自用不定时更新
Talking about the wavelength of laser radar