• Java--异常/Exception--try/catch/finally的return顺序


    原文网址:Java--异常/Exception--try/catch/finally的return顺序_IT利刃出鞘的博客-CSDN博客

    简介

    简介

    本文介绍Java中捕获异常时是否return对程序流程的影响。

    总结

    1. 如果catch或者finally中有return,则catch和finally代码块之后的部分根本不会执行到。
    2. 如果catch和finally中都有return,后边的(也就是finally的)return会作为返回值。
    3. try,catch执行到了return之前都会执行finally
    4. 不要在try里return。

    正常用法(try异常, catch/finally无return)

    1. package org.example.a;
    2. public class Demo {
    3. public static void main(String[] args) {
    4. Object handler = handler();
    5. System.out.println(handler.toString());
    6. }
    7. public static Object handler() {
    8. try {
    9. System.out.println("try:内(前)");
    10. System.out.println("try:内(异常)" + 5 / 0);
    11. System.out.println("try:内(后)");
    12. return "try:返回";
    13. } catch (Exception e) {
    14. System.out.println("catch:内(前)");
    15. // System.out.println("catch:内(异常)" + 5 / 0);
    16. // System.out.println("catch:内(后)");
    17. // return "catch:返回";
    18. } finally {
    19. System.out.println("finally:内");
    20. // return "finally:返回";
    21. }
    22. System.out.println("最后");
    23. return "最后(返回)";
    24. }
    25. }

    执行结果(try=> catch=> finally=> finally块之外)

    1. try:内(前)
    2. catch:内(前)
    3. finally:内
    4. 最后
    5. 最后(返回)

    try无异常, catch有return, finally无return

    1. package org.example.a;
    2. public class Demo {
    3. public static void main(String[] args) {
    4. Object handler = handler();
    5. System.out.println(handler.toString());
    6. }
    7. public static Object handler() {
    8. try {
    9. System.out.println("try:内(前)");
    10. // System.out.println("try:内(异常)" + 5 / 0);
    11. // System.out.println("try:内(后)");
    12. return "try:返回";
    13. } catch (Exception e) {
    14. System.out.println("catch:内(前)");
    15. System.out.println("catch:内(异常)" + 5 / 0);
    16. System.out.println("catch:内(后)");
    17. return "catch:返回";
    18. } finally {
    19. System.out.println("finally:内");
    20. // return "finally:返回";
    21. }
    22. }
    23. }

    执行结果(try=> finally=> try的return)

    1. try:内(前)
    2. finally:内
    3. try:返回

    try无异常, finally有return(Idea报警告)

    1. package org.example.a;
    2. public class Demo {
    3. public static void main(String[] args) {
    4. Object handler = handler();
    5. System.out.println(handler.toString());
    6. }
    7. public static Object handler() {
    8. try {
    9. System.out.println("try:内(前)");
    10. // System.out.println("try:内(异常)" + 5 / 0);
    11. // System.out.println("try:内(后)");
    12. return "try:返回";
    13. } catch (Exception e) {
    14. System.out.println("catch:内(前)");
    15. System.out.println("catch:内(异常)" + 5 / 0);
    16. System.out.println("catch:内(后)");
    17. return "catch:返回";
    18. } finally {
    19. System.out.println("finally:内");
    20. return "finally:返回";
    21. }
    22. }
    23. }

    执行结果(try=> finally=> 程序结束(不调用try的return))

    1. try:内(前)
    2. finally:内
    3. finally:返回

    try有异常, catch有return, finally无return

    1. package org.example.a;
    2. public class Demo {
    3. public static void main(String[] args) {
    4. Object handler = handler();
    5. System.out.println(handler.toString());
    6. }
    7. public static Object handler() {
    8. try {
    9. System.out.println("try:内(前)");
    10. System.out.println("try:内(异常)" + 5 / 0);
    11. System.out.println("try:内(后)");
    12. return "try:返回";
    13. } catch (Exception e) {
    14. System.out.println("catch:内(前)");
    15. // System.out.println("catch:内(异常)" + 5 / 0);
    16. // System.out.println("catch:内(后)");
    17. return "catch:返回";
    18. } finally {
    19. System.out.println("finally:内");
    20. // return "finally:返回";
    21. }
    22. }
    23. }

    执行结果(try=> catch=> finally=> catch的return)

    1. try:内(前)
    2. catch:内(前)
    3. finally:内
    4. catch:返回

    try有异常, catch有return, finally有return

    1. package org.example.a;
    2. public class Demo {
    3. public static void main(String[] args) {
    4. Object handler = handler();
    5. System.out.println(handler.toString());
    6. }
    7. public static Object handler() {
    8. try {
    9. System.out.println("try:内(前)");
    10. System.out.println("try:内(异常)" + 5 / 0);
    11. System.out.println("try:内(后)");
    12. return "try:返回";
    13. } catch (Exception e) {
    14. System.out.println("catch:内(前)");
    15. // System.out.println("catch:内(异常)" + 5 / 0);
    16. // System.out.println("catch:内(后)");
    17. return "catch:返回";
    18. } finally {
    19. System.out.println("finally:内");
    20. return "finally:返回";
    21. }
    22. }
    23. }

    执行结果(try=> catch=> finally=> 程序退出

    1. try:内(前)
    2. catch:内(前)
    3. finally:内
    4. finally:返回

    try有异常, catch有异常, finally无return

    1. package org.example.a;
    2. public class Demo {
    3. public static void main(String[] args) {
    4. Object handler = handler();
    5. System.out.println(handler.toString());
    6. }
    7. public static Object handler() {
    8. try {
    9. System.out.println("try:内(前)");
    10. System.out.println("try:内(异常)" + 5 / 0);
    11. System.out.println("try:内(后)");
    12. return "try:返回";
    13. } catch (Exception e) {
    14. System.out.println("catch:内(前)");
    15. System.out.println("catch:内(异常)" + 5 / 0);
    16. System.out.println("catch:内(后)");
    17. return "catch:返回";
    18. } finally {
    19. System.out.println("finally:内");
    20. // return "finally:返回";
    21. }
    22. }
    23. }

    执行结果(try=> catch=> finally=> 程序结束(catch不会再return))

    1. Exception in thread "main" java.lang.ArithmeticException: / by zero
    2. at org.example.a.Demo.handler(Demo.java:18)
    3. at org.example.a.Demo.main(Demo.java:5)
    4. try:内(前)
    5. catch:内(前)
    6. finally:内

  • 相关阅读:
    Package hyperref Warning: Ignoring empty anchor on input line 202.
    二叉树—相关计算题
    NX二次开发-一个简单的连接曲线例子剖析学会如何使用NXOPEN做二次开发
    strace应用
    记录:2022-9-13 正则表达式匹配 Java中的String能有多大 文件系统目录 原子性操作 锁 同步 信号量
    Selenium 模拟浏览器操作案例
    java计算机毕业设计家庭饮食营养管理MyBatis+系统+LW文档+源码+调试部署
    Set接口和常用方法
    C# List集合赋值
    Java 诊断工具 Arthas 基础教程
  • 原文地址:https://blog.csdn.net/feiying0canglang/article/details/126937315