003-try-catch-finally-return执行顺序问题
2024-10-19 06:10:56
一、try-catch-finally-return执行顺序问题
0、原始执行顺序
try — > finally
try —> catch —> finally
1、try catch 中有 return,finally 中无 return,且 try 中无异常抛出
public int add(){
int i = 1;
try{
i = i + 1;
System.out.println("try: i = " + i);
return i;
}catch (Exception e){
i = i + 2;
System.out.println("exception: i = " + i);
return i;
}finally {
i = i + 3;
System.out.println("finally: i = " + i);
}
}
/**
执行结果:
try: i = 2
finally: i = 5
2
*/
2、try catch finally中均有 return,try中无异常抛出
public int add(){
int i = 1;
try{
i = i + 1;
System.out.println("try: i = " + i);
return i;
}catch (Exception e){
i = i + 2;
System.out.println("exception: i = " + i);
return i;
}finally {
i = i + 3;
System.out.println("finally: i = " + i);
return i;
}
}
/**
返回结果5
try: i = 2
finally: i = 5
5
*/
3、try catch 中有 return,finally 中无 return,且 try 中有异常抛出
public int add(){
int i = 1;
try{
i = i + 1;
int a = i/0;
System.out.println("try: i = " + i);
return i;
}catch (Exception e){
i = i + 2;
System.out.println("exception: i = " + i);
return i;
}finally {
i = i + 3;
System.out.println("finally: i = " + i);
}
}
/**
返回结果4
exception: i = 4
finally: i = 7
4
*/
4、try catch finally中均有 return,try中有异常抛出
public int add(){
int i = 1;
try{
i = i + 1;
int a = i/0;
System.out.println("try: i = " + i);
return i;
}catch (Exception e){
i = i + 2;
System.out.println("exception: i = " + i);
return i;
}finally {
i = i + 3;
System.out.println("finally: i = " + i);
return i;
}
}
/**
返回结果5
exception: i = 4
finally: i = 7
7
*/
5、try、catch 中有 return,try finally 中有异常,finally 中的异常有 return
public int add(){
int i = 1;
try{
i = i + 1;
int a = i/0;
System.out.println("try: i = " + i);
return i;
}catch (Exception e){
i = i + 2;
System.out.println("exception: i = " + i);
return i;
}finally {
i = i + 3;
try {
int b = i/0;
} catch (Exception e) {
i = i + 5;
return i;
}
System.out.println("finally: i = " + i);
}
}
/**
exception: i = 4
12
*/
6、try、catch 中有 return,try finally 中有异常,finally 中的异常无 return
public int add(){
int i = 1;
try{
i = i + 1;
int a = i/0;
System.out.println("try: i = " + i);
return i;
}catch (Exception e){
i = i + 2;
System.out.println("exception: i = " + i);
return i;
}finally {
i = i + 3;
try {
int b = i/0;
} catch (Exception e) {
i = i + 5;
}
System.out.println("finally: i = " + i);
}
}
/**
exception: i = 4
finally: i = 12
4
*/
总结
- finally 的代码总会被执行
- try、catch 中有 return 的时候,也会执行 finally。return 的时候,要注意返回值是否会受到 finally 中代码的影响。
- finally 中有 return 的时候,会直接在 finally 中退出,导致 try、catch中的 return 失效。
- finally 中如果有异常的时候,且存在 return 的时候,catch 的 return 会被覆盖。
最新文章
- memcached安装配置+基础操作
- 正则表达式 判断 ip:端口 形式
- MySQL 存储过程
- “耐撕”团队第一次讨论——“抢答器”需求分析
- jquery easyui 弹出消息框
- UIScrollerView常见属性
- cocos2dx3.0-执行cocos compile -p win32 命令出现错误 MSB8020 以及编译出来的exe 无法打开的问题
- 使用ef code first模式,在部署后服务器端把数据库同步到最新版本的方法
- HTML5中class选择器属性的解释
- Github创建分支
- The Beginning of the Graph Theory
- SQL*Loader FAQ
- Python全栈(第一部分)day2
- css布局 - 两栏自适应布局的几种实现方法汇总
- 「SNOI2019」通信
- 4、Ubuntu系统环境变量详解
- jquery easy ui 简单字段选择搜索实现
- Excel 函数使用
- uvaoj 1081510815 - Andy's First Dictionary(set应用)
- [转]Insert, Update, and Delete Destination table with SSIS
热门文章
- 【算法】KMP算法
- ysoserial Commons Collections1反序列化研究
- 银河麒麟V10安装ASP.NET Core并配置Supervisor让网站开机自动运行
- SwiftUI All In One
- js data type checker
- .bashrc &; rename hostname &; rename username
- 蓝牙鼠标 &; 罗技 M337
- 为什么要抢挖Baccarat流动性挖矿的头矿?头矿的价值是什么?
- ASP.NET Core中如何对不同类型的用户进行区别限流
- [转]Ubuntu16.04安装搜狗输入法