一、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 会被覆盖。

最新文章

  1. memcached安装配置+基础操作
  2. 正则表达式 判断 ip:端口 形式
  3. MySQL 存储过程
  4. “耐撕”团队第一次讨论——“抢答器”需求分析
  5. jquery easyui 弹出消息框
  6. UIScrollerView常见属性
  7. cocos2dx3.0-执行cocos compile -p win32 命令出现错误 MSB8020 以及编译出来的exe 无法打开的问题
  8. 使用ef code first模式,在部署后服务器端把数据库同步到最新版本的方法
  9. HTML5中class选择器属性的解释
  10. Github创建分支
  11. The Beginning of the Graph Theory
  12. SQL*Loader FAQ
  13. Python全栈(第一部分)day2
  14. css布局 - 两栏自适应布局的几种实现方法汇总
  15. 「SNOI2019」通信
  16. 4、Ubuntu系统环境变量详解
  17. jquery easy ui 简单字段选择搜索实现
  18. Excel 函数使用
  19. uvaoj 1081510815 - Andy's First Dictionary(set应用)
  20. [转]Insert, Update, and Delete Destination table with SSIS

热门文章

  1. 【算法】KMP算法
  2. ysoserial Commons Collections1反序列化研究
  3. 银河麒麟V10安装ASP.NET Core并配置Supervisor让网站开机自动运行
  4. SwiftUI All In One
  5. js data type checker
  6. .bashrc & rename hostname & rename username
  7. 蓝牙鼠标 & 罗技 M337
  8. 为什么要抢挖Baccarat流动性挖矿的头矿?头矿的价值是什么?
  9. ASP.NET Core中如何对不同类型的用户进行区别限流
  10. [转]Ubuntu16.04安装搜狗输入法