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

003-try-catch-finally-return执行顺序问题的更多相关文章

  1. 【转】C# 异常处理 throw和throw ex的区别 try catch finally的执行顺序(return)

    [转]throw和throw ex的区别 之前,在使用异常捕获语句try...catch...throw语句时,一直没太留意几种用法的区别,前几天调试程序时无意中了解到几种使用方法是有区别的,网上一查 ...

  2. try catch finally return运行顺序

    首先让我们搞懂两组概念:try catch finally和return 1.try catch finally 首先说try catch, (1)try语句 ,try语句用来包围可能出现异常的代码片 ...

  3. 异常 try catch finally return 执行关系 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  4. 有return的情况下try catch finally的执行顺序(转)

    结论:1.不管有木有出现异常,finally块中代码都会执行:2.当try和catch中有return时,finally仍然会执行:3.finally是在return后面的表达式运算后执行的(此时并没 ...

  5. try catch finally的执行顺序(有return的情况下)

    结论:1.不管有木有出现异常,finally块中代码都会执行:2.当try和catch中有return时,finally仍然会执行:3.finally是在return后面的表达式运算后执行的(此时并没 ...

  6. 有return的情况下try catch finally的执行顺序

    结论:1.不管有木有出现异常,finally块中代码都会执行:2.当try和catch中有return时,finally仍然会执行:3.finally是在return后面的表达式运算后执行的(此时并没 ...

  7. 有return的情况下try catch finally的执行顺序(最有说服力的总结)

    结论:1.不管有木有出现异常,finally块中代码都会执行:2.当try和catch中有return时,finally仍然会执行:3.finally是在return后面的表达式运算后执行的(此时并没 ...

  8. Java基础知识强化之IO流笔记06:有return的情况下try catch finally的执行顺序

    1. 给出结论: (1)不管有木有出现异常,finally块中代码都会执行:(2)当try和catch中有return时,finally仍然会执行:(3)finally是在return后面的表达式运算 ...

  9. 【Java疑难杂症】有return的情况下try catch finally的执行顺序

    有这样一个问题,异常处理大家应该都不陌生,类似如下代码: public class Test { public static void main(String[] args) { int d1 = 0 ...

  10. 有return的情况下try catch finally的执行顺序(转)

    结论:1.不管有没有出现异常,finally块中代码都会执行:2.当try和catch中有return时,finally仍然会执行:3.finally是在return后面的表达式运算后执行的(此时并没 ...

随机推荐

  1. CF1463-D. Pairs

    CF1463-D. Pairs 题意: 有从\(1\)到\(2n\)一共\(2n\)个数字,让你将这\(2n\)个数字分成\(n\)组,每组有两个数字.对于这\(n\)组数字,你可以从中挑选\(x\) ...

  2. Redis 事务 & 消息队列

    Redis 消息队列介绍 什么是消息队列 消息队列(Message Queue)是一种应用间的通信方式,消息发送后可以立即返回,有消息系统来确保信息的可靠传递,消息生产者只管把消息发布到消息队列中而不 ...

  3. Hexo准备---Node.js、Vue

    Hexo准备---Node.js.Vue 安装node.js 1.下载node 配置node.js环境官网下载,一直next就好,非常方便. 下载官网: http://nodejs.cn/downlo ...

  4. codeforces 869A

    A. The Artful Expedient time limit per test 1 second memory limit per test 256 megabytes input stand ...

  5. C# 类 (12) - Partial

    Partial 前面说了,同一个namespace 里 class 名字是不能重的,除非是在不同的namespace里,下面开始打脸在同一个namespace里,加上partial 关键字,可以写同样 ...

  6. Github OAuth All In One

    Github OAuth All In One new https://docs.github.com/en/free-pro-team@latest/developers/apps/authoriz ...

  7. npm & private npm service & nrm & nvm

    npm & private npm service & nrm & nvm npm server # nrm https://www.cnblogs.com/xgqfrms/t ...

  8. 口罩 & 防毒面具 N95 & P100

    口罩 & 防毒面具 N95 & P100 N95 口罩 < 防毒面具 P100 https://www.techritual.com/2020/01/30/210599/

  9. vux使用

    Vue中使用vux的配置,分为两种情况: 一.根据vux文档直接安装,无需手动配置 npm install vue-cli -g // 如果还没安装 vue init airyland/vux2 my ...

  10. H5 常见问题汇总及解决方案

    原文链接:http://mp.weixin.qq.com/s/JVUpsz9QHsNV0_7U-3HCMg H5 项目常见问题汇总及解决方案 -- 由钟平勇分享 转自 https://github.c ...