Java深入学习23:try catch finally执行和返回逻辑

Posted 我不吃番茄

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java深入学习23:try catch finally执行和返回逻辑相关的知识,希望对你有一定的参考价值。

Java深入学习23:try catch finally执行和返回逻辑

 

先说结论:

  一般逻辑,先执行try语句;如果有异常,则继续执行catch语句;如果有finally语句,则执行finally语句;

  任何执行try 或者catch中的return语句之前,如果finally存在的,都会先执行finally语句;

  如果finally中有return语句,那么程序就在finally中return了,所以finally中的return是一定会被return的,否则

 

源码示例:

public class TryCatchTest {

    public static String testNormalWithFinally(){

        try {
            int i = 0/1;
            System.out.println("this is try block");
            return "return try";
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("this is catch block");
            return "return catch";
        } finally {
            System.out.println("this is finally block");
            return "return finally ";
        }

    }

    public static String testExceptionWithFinally(){

        try {
            int j = 1/0;
            System.out.println("this is ctry block");
            return "return try";
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("this is catch block");
            return "return catch";
        } finally {
            System.out.println("this is finally block");
            return "return finally ";
        }

    }
    public static String testNormalWithoutFinallyReturn(){

        try {
            int i = 0/1;
            System.out.println("this is try block");
            return "return try";
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("this is catch block");
            return "return catch";
        } finally {
            System.out.println("this is finally block");
        }

    }
    public static String testExceptionWithoutFinallyReturn(){

        try {
            int j = 1/0;
            System.out.println("this is ctry block");
            return "return try";
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("this is catch block");
            return "return catch";
        } finally {
            System.out.println("this is finally block");
        }

    }

    public static void main(String[] args) {
        System.out.println("-------------testNormalWithFinally-------------------");
        System.out.println(testNormalWithFinally());
        System.out.println("----------------testExceptionWithFinally----------------");
        System.out.println(testExceptionWithFinally());
        System.out.println("---------------testNormalWithoutFinallyReturn-----------------");
        System.out.println(testNormalWithoutFinallyReturn());
        System.out.println("--------------testExceptionWithoutFinallyReturn------------------");
        System.out.println(testExceptionWithoutFinallyReturn());
    }
}
--------------------------------日志输出---------------------------------------------
-------------testNormalWithFinally-------------------
java.lang.ArithmeticException: / by zero
this is try block
    at interview.trycatchfinally.TryCatchTest.testExceptionWithFinally(TryCatchTest.java:32)
this is finally block
return finally 
    at interview.trycatchfinally.TryCatchTest.main(TryCatchTest.java:80)
----------------testExceptionWithFinally----------------
java.lang.ArithmeticException: / by zero
this is catch block
this is finally block
return finally 
    at interview.trycatchfinally.TryCatchTest.testExceptionWithoutFinallyReturn(TryCatchTest.java:63)
---------------testNormalWithoutFinallyReturn-----------------
this is try block
    at interview.trycatchfinally.TryCatchTest.main(TryCatchTest.java:84)
this is finally block
return try
--------------testExceptionWithoutFinallyReturn------------------
this is catch block
this is finally block
return catch

 

以上是关于Java深入学习23:try catch finally执行和返回逻辑的主要内容,如果未能解决你的问题,请参考以下文章

Java异常之try,catch,finally,throw,throws

一个try可以跟进多个catch语句,用于处理不同情况,当一个try只能匹配一个catch

深入剖析java的try…catch…finally语句

深入理解Java中的final关键字(转)

Java异常处理机制 —— 深入理解与开发应用

Java中的异常处理机制