• 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:内

  • 相关阅读:
    Java中集合知识点
    从XXL-job路由策略的“服务容错“说起
    ViTPose+:迈向通用身体姿态估计的视觉Transformer基础模型 | 京东探索研究院
    【祝福伟大的祖国】Java Web 9.2 Request 对象 9.2.6 Request 请求转发
    javaweb JAVA JSP聊天室程序源码(局域网聊天系统 即时通讯)网页聊天系统
    【计算机网络】网络层(二)—— 路由选择协议
    【课程设计|MFC】学生成绩分析系统(含课程报告+源码)
    linux个别命令笔记
    webpack:自定义plugin插件开发
    python 变量引用,值变量
  • 原文地址:https://blog.csdn.net/feiying0canglang/article/details/126937315