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

  • 相关阅读:
    Lua--基础入门
    gitflow工作流程思路
    【Android - 技术期刊】第002期
    Tomcat服务部署、优化
    高清免费壁纸网站推荐
    计算机基础知识56
    计算机毕业设计(附源码)python作业管理系统设计
    订单超时自动取消订单实现策略
    1、【gradio】快速开始,构建自己的机器学习的应用
    实现制作动漫版的你---动漫风格迁移网络AnimeGANv2
  • 原文地址:https://blog.csdn.net/feiying0canglang/article/details/126937315