补充:try-catch-finally 中 return 的执行顺序

文章详细探讨了在Java中使用try-catch-finally语句块时,不同情况下return语句和对局部变量的操作。总结了五个场景,包括try、catch、finally中return的存在与否以及是否修改局部变量,展示了finally块的执行顺序优先级和对返回值的影响。
摘要由CSDN通过智能技术生成

1. try-catch-finally 后面

private void scene01() {
    try {
        System.out.println("--try");
    } catch (Exception ex) {
        System.out.println("--catch");
    } finally {
        System.out.println("--finally");
    }
    System.out.println("--after");
    return;
}
--try
--finally
--after
  • 结论:程序按顺序执行。

2. try-catch-finally 后面、try 里面

不操作局部变量

private void scene02() {
    try {
        System.out.println("--try");
        return;
    } catch (Exception ex) {
        System.out.println("--catch");
    } finally {
        System.out.println("--finally");
    }
    System.out.println("--after");
    return;
}
--try
--finally
  • 结论:
    • 程序执行 try 块中 return 之前代码。
    • 再执行 finally 块,最后执行 try 中 return。
  • 注意:
    • try-catch-finally 块之后的 return,因为程序在 try 中已经 return,所以不再执行。

操作局部变量

private String scene02_1() {
    String result = "";
    try {
        result = "try";
        return result;
    } finally {
        result = "finally";
    }
    return result;
}
try
  • 结论:当 try 和 finally 同时操作某变量时,try 里有 return 但 finally 里没有 return 时,finally 的代码不会改变变量的值

3. try-catch-finally 后面、try 抛异常、catch 里面

不操作局部变量

private void scene03() {
    try {
        System.out.println("--try");
        if (1 < 2) {
            throw new RuntimeException();
        }
    } catch (Exception ex) {
        System.out.println("--catch");
        return;
    } finally {
        System.out.println("--finally");
    }
    System.out.println("--after");
    return;
}
--try
--catch
--finally
  • 结论:执行 catch 的 return 前执行 finally 中的内容。

操作局部变量

private String scene03_1() {
    String result = "";
    try {
        if (1 < 2) {
            throw new RuntimeException();
        }
    } catch (Exception ex) {
        result = "catch";
        return result;
    } finally {
        result = "finally";
    }
    return result;
}
catch
  • 结论:当 catch 和 finally 同时操作某变量时,catch 里有 return 但 finally 里没有 return 时,finally 的代码不会改变变量的值。

4. try 里面、finally 里面

不操作局部变量

private void scene04() {
    try {
        System.out.println("--try");
        return;
    } catch (Exception ex) {
        System.out.println("--catch");
    } finally {
        System.out.println("--finally");
        return;
    }
}
--try
--finally
  • 结论:
    • 程序执行 try 块中 return 之前代码。
    • 再执行 finally 块,因为 finally 块中有 return 所以提前退出。
  • 注意:try 的 return 不再执行!出口是 finally 的 return。

操作局部变量

private String scene04_1() {
    String result = "";
    try {
        result = "try";
        return result;
    } finally {
        result = "finally";
        return result;
    }
}
finally
  • 结论:当 try 和 finally 同时操作某变量时,try 里有 return 且 finally 里也有 return 时,finally 的代码会改变变量的值。

5. catch 里面、finally 里面

不操作局部变量

private void scene05() {
    try {
        if (1 < 2) {
            throw new RuntimeException();
        }
        System.out.println("--try");
    } catch (Exception ex) {
        System.out.println("--catch");
        return;
    } finally {
        System.out.println("--finally");
        return;
    }
}
--catch
--finally
  • 结论:
    • 程序执行 catch 块中 return 之前代码;
    • 再执行 finally 块,因为finally块中有 return 所以提前退出。
  • 注意:catch 的 return 不再执行!出口是 finally 的 return。

操作局部变量

private String scene05_1() {
    String result = "";
    try {
        if (1 < 2) {
            throw new RuntimeException();
        }
    } catch (Exception ex) {
        result = "catch";
        return result;
    } finally {
        result = "finally";
        return result;
    }
}
finally
  • 结论:当 catch 和 finally 同时操作某变量时,catch 里有 return 且 finally 里也有 return 时,finally 的代码会改变变量的值。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值