当前位置:网站首页>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
边栏推荐
- SQL injection case demonstration and preventive measures
- try-catch finally执行顺序的例题
- 任总与系统工程领域科学家、专家会谈纪要
- Lambda终结操作查找与匹配findFirst
- leetcode.1 --- 两数之和
- 【 développement d'essais 】 sélénium d'essais automatisés (Ⅲ) - - analyse du cadre unitest
- [MySQL] index and transaction
- Fundamentals of robot obstacle avoidance system
- 单片机:Modbus 通信协议介绍
- Use of Oracle PL-SQL
猜你喜欢

UDP connection map collection

【测试开发】用例篇

Detailed explanation of MySQL storage process

EGO Planner代码解析----CMakeLists.txt和package.xml
![[multithreading] what is multithreading in the end -- the elementary level of multithreading (review for self use)](/img/87/71111f1753b8b183f60705177bfc57.png)
[multithreading] what is multithreading in the end -- the elementary level of multithreading (review for self use)

【测试开发】测试的相关基本概念

SQL injection case demonstration and preventive measures

Fundamentals of robot obstacle avoidance system

单片机:PCF8591硬件接口
![[test development] file compression project practice](/img/4f/3bbbde5953f8d97bbe4460c63f13c7.png)
[test development] file compression project practice
随机推荐
UnionPay commerce - merchant statistics service platform
缓存读写--写
Lambda termination operation find and match nonematch
【测试开发】自动化测试selenium(三)——unittest框架解析
LVS四层负载均衡集群(3)集群功能分类 - HPC
Review the first three IO streams
Local simulation download file
2022 spring semester summary
[test development] automatic test selenium (I)
Oracle database management
Wechat payment configuration
Line height equals height why not center
微信扫描二维码无法下载文件的解决办法
单片机信号发生器程序
[interview review] update from time to time for personal use
Multi thread writing method and the making of slot machine
Lambda终结操作count
基于PHP的轻量数码商城系统
单片机:A/D 和 D/A 的基本概念
Use lodash to merge the values of the same fields in an array object