原文网址:Java--异常/Exception--try/catch/finally的return顺序_IT利刃出鞘的博客-CSDN博客
简介
本文介绍Java中捕获异常时是否return对程序流程的影响。
总结
- package org.example.a;
-
- public class Demo {
- public static void main(String[] args) {
- Object handler = handler();
- System.out.println(handler.toString());
- }
-
- public static Object handler() {
- try {
- System.out.println("try:内(前)");
- System.out.println("try:内(异常)" + 5 / 0);
- System.out.println("try:内(后)");
-
- return "try:返回";
- } catch (Exception e) {
- System.out.println("catch:内(前)");
- // System.out.println("catch:内(异常)" + 5 / 0);
- // System.out.println("catch:内(后)");
- // return "catch:返回";
- } finally {
- System.out.println("finally:内");
- // return "finally:返回";
- }
- System.out.println("最后");
- return "最后(返回)";
- }
- }
执行结果(try=> catch=> finally=> finally块之外)
- try:内(前)
- catch:内(前)
- finally:内
- 最后
- 最后(返回)
- package org.example.a;
-
- public class Demo {
- public static void main(String[] args) {
- Object handler = handler();
- System.out.println(handler.toString());
- }
-
- public static Object handler() {
- try {
- System.out.println("try:内(前)");
- // System.out.println("try:内(异常)" + 5 / 0);
- // System.out.println("try:内(后)");
-
- return "try:返回";
- } catch (Exception e) {
- System.out.println("catch:内(前)");
- System.out.println("catch:内(异常)" + 5 / 0);
- System.out.println("catch:内(后)");
-
- return "catch:返回";
- } finally {
- System.out.println("finally:内");
- // return "finally:返回";
- }
- }
- }
执行结果(try=> finally=> try的return)
- try:内(前)
- finally:内
- try:返回
- package org.example.a;
-
- public class Demo {
- public static void main(String[] args) {
- Object handler = handler();
- System.out.println(handler.toString());
- }
-
- public static Object handler() {
- try {
- System.out.println("try:内(前)");
- // System.out.println("try:内(异常)" + 5 / 0);
- // System.out.println("try:内(后)");
-
- return "try:返回";
- } catch (Exception e) {
- System.out.println("catch:内(前)");
- System.out.println("catch:内(异常)" + 5 / 0);
- System.out.println("catch:内(后)");
-
- return "catch:返回";
- } finally {
- System.out.println("finally:内");
- return "finally:返回";
- }
- }
- }
执行结果(try=> finally=> 程序结束(不调用try的return))
- try:内(前)
- finally:内
- finally:返回
- package org.example.a;
-
- public class Demo {
- public static void main(String[] args) {
- Object handler = handler();
- System.out.println(handler.toString());
- }
-
- public static Object handler() {
- try {
- System.out.println("try:内(前)");
- System.out.println("try:内(异常)" + 5 / 0);
- System.out.println("try:内(后)");
-
- return "try:返回";
- } catch (Exception e) {
- System.out.println("catch:内(前)");
- // System.out.println("catch:内(异常)" + 5 / 0);
- // System.out.println("catch:内(后)");
-
- return "catch:返回";
- } finally {
- System.out.println("finally:内");
- // return "finally:返回";
- }
- }
- }
执行结果(try=> catch=> finally=> catch的return)
- try:内(前)
- catch:内(前)
- finally:内
- catch:返回
- package org.example.a;
-
- public class Demo {
- public static void main(String[] args) {
- Object handler = handler();
- System.out.println(handler.toString());
- }
-
- public static Object handler() {
- try {
- System.out.println("try:内(前)");
- System.out.println("try:内(异常)" + 5 / 0);
- System.out.println("try:内(后)");
-
- return "try:返回";
- } catch (Exception e) {
- System.out.println("catch:内(前)");
- // System.out.println("catch:内(异常)" + 5 / 0);
- // System.out.println("catch:内(后)");
-
- return "catch:返回";
- } finally {
- System.out.println("finally:内");
- return "finally:返回";
- }
- }
- }
执行结果(try=> catch=> finally=> 程序退出)
- try:内(前)
- catch:内(前)
- finally:内
- finally:返回
- package org.example.a;
-
- public class Demo {
- public static void main(String[] args) {
- Object handler = handler();
- System.out.println(handler.toString());
- }
-
- public static Object handler() {
- try {
- System.out.println("try:内(前)");
- System.out.println("try:内(异常)" + 5 / 0);
- System.out.println("try:内(后)");
-
- return "try:返回";
- } catch (Exception e) {
- System.out.println("catch:内(前)");
- System.out.println("catch:内(异常)" + 5 / 0);
- System.out.println("catch:内(后)");
-
- return "catch:返回";
- } finally {
- System.out.println("finally:内");
- // return "finally:返回";
- }
- }
- }
执行结果(try=> catch=> finally=> 程序结束(catch不会再return))
- Exception in thread "main" java.lang.ArithmeticException: / by zero
- at org.example.a.Demo.handler(Demo.java:18)
- at org.example.a.Demo.main(Demo.java:5)
- try:内(前)
- catch:内(前)
- finally:内