TryCatchFinally+return 总结

Posted sshhsun-

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TryCatchFinally+return 总结相关的知识,希望对你有一定的参考价值。

finally中有return:如果try正常/或异常,则执行finally中的return


如果finally中没有return, 如果try正常,执行tryreturn语句,
如果异常执行catch中return语句,但是此事return语句返回在finally语句之后


finally中有错误,应该是直接退出

—上代码!!

package others;

/**
 * finally中有return:如果try正常/或异常,则执行finally中的return
 * 
 * 
 * 如果finally中没有return, 如果try正常,执行tryreturn语句,
 * 如果异常执行catch中return语句,但是此事return语句返回在finally语句之后
 * 
 * 
 * finally中有错误,应该是直接退出
 * 
 * @author sshunsun
 *
 */
public class TryCatchFinally 
    public static void main(String[] args) 
        System.out.println(Test1(1));
        System.out.println("------------------");
        System.out.println(Test1(2));
        System.out.println("~~~~~~~~~~~~~~~");
        System.out.println(Test2(1));
        System.out.println("------------------");
        System.out.println(Test2(2));
        System.out.println("~~~~~~~~~~~~~~~");
        System.out.println(Test3(1));
    

    /**
     * @param num
     * @return finally中有return:如果try正常/或异常,则执行finally中的return
     * 
     */
    public static String Test1(int num) 
        System.out.println("Test1----" + num);
        try 
            System.out.println("start Try");
            if (num == 2) 
                throw new Exception(num + "");
            
            System.out.println("end Try");
            return "return in try";
         catch (Exception e) 
            System.out.println("start Catch");

            System.out.println("end Catch");
            return "return in catch";
         finally 

            System.out.println("start Finally");

            System.out.println("end Finally");
            return "return in finally";
        
    





    /**
     * @param num
     * @return 如果finally中没有return, 如果try正常,执行tryreturn语句,
     *         如果异常执行catch中return语句,但是此事return语句返回在finally语句之后
     *         finally对最终结果不会有影响!!
     */
    public static String Test2(int num) 
        int i = num;
        System.out.println("Test2----" + num);
        try 
            System.out.println("start Try");
            if (num == 2) 
                throw new Exception(num + "");
            
            System.out.println("end Try");
            return "return in try  num:" + i;
         catch (Exception e) 
            System.out.println("start Catch");

            System.out.println("end Catch");
            return "return in catch  num:" + i;
         finally 
            System.out.println("start Finally");
            i = 0; // finally对最终结果不会有影响
            System.out.println("end Finally");
        
    




    /**
     * @param num
     * @return
     * 
     * 
     *         finally中有错误,应该是直接退出
     */
    public static String Test3(int num) 
        int i = num;
        System.out.println("Test3----" + num);
        try 
            System.out.println("start Try");
            if (num == 2) 
                throw new Exception(num + "");
            
            System.out.println("end Try");
            return "return in try  num:" + i;
         catch (Exception e) 
            System.out.println("start Catch");

            System.out.println("end Catch");
            return "return in catch  num:" + i;
         finally 
            System.out.println("start Finally");
            int t = 5 / 0;
            System.out.println("end Finally");

            return "return in finally";
        
    


运行结果:

Test1----1
start Try
end Try
start Finally
end Finally
return in finally
------------------
Test1----2
start Try
start Catch
end Catch
start Finally
end Finally
return in finally
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Test2----1
start Try
end Try
start Finally
end Finally
return in try  num:1
------------------
Test2----2
start Try
start Catch
end Catch
start Finally
end Finally
return in catch  num:2
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Test3----1
start Try
end Try
start Finally
Exception in thread "main" java.lang.ArithmeticException: / by zero
    at others.TryCatchFinally.Test3(TryCatchFinally.java:99)
    at others.TryCatchFinally.main(TryCatchFinally.java:23)

以上是关于TryCatchFinally+return 总结的主要内容,如果未能解决你的问题,请参考以下文章

JAVA中trycatchfinally带return的执行顺序总结

trycatchfinally用法总结

Java笔试面试总结—trycatchfinally语句中有return 的各类情况

trycatchfinally容易忽略的点

trycatchfinally 执行顺序

转case: Java中try catch finally语句中含有return语句的执行情况(总结版)