Java-- 异常--try/catch/finally 的执行顺序

直接说结论:

  1. catch或者finally中有return语句时,catchfinally代码块之后的程序部分将不会被执行到。
  2. 如果catchfinally中都存在return语句,最终的返回值将是finally中的return语句所指定的值。
  3. 无论try代码块中是否出现异常,只要trycatch执行到了return之前,finally代码块都会被执行。
  4. 为了代码的可读性和健壮性,尽量避免在try代码块中使用return语句。

不同情况的代码示例及执行结果分析

        1.正常用法(try 异常,catch/finally 无 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:内
最后
最后(返回)

分析:try中出现异常,跳转到catch执行,catch执行完后执行finallyfinally执行完后继续执行finally之后的代码。

        2.try 无异常,catch 有 return, finally 无 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:返回";
        }
    }
}

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

try:内(前)
finally:内
try:返回

分析:try中无异常,执行到try中的return之前先执行finally,然后返回try中的return值。

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

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:返回

分析:try中无异常,执行到try中的return之前先执行finallyfinally中有return,直接返回finally中的值,不再执行try中的return

        4.try 有异常,catch 有 return, finally 无 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:返回";
        }
    }
}

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

try:内(前)
catch:内(前)
finally:内
catch:返回

分析:try中出现异常,跳转到catch执行,catch中有return,在执行catch中的return之前先执行finally,然后返回catch中的值。

        5.try 有异常,catch 有 return, finally 有 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:返回";
        }
    }
}

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

try:内(前)
catch:内(前)
finally:内
finally:返回

分析:try中出现异常,跳转到catch执行,catch中有return,在执行catch中的return之前先执行finallyfinally中也有return,最终返回finally中的值。

        6.try 有异常,catch 有异常,finally 无 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:返回";
        }
    }
}

执行结果(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:内

分析:try中出现异常,跳转到catch执行,catch中又出现异常,在执行catch中的return之前先执行finally,由于catch中的异常未被处理,导致程序终止。

你可能感兴趣的:(Java后端开发面试题,Java,java,开发语言,面试)