Java throw try catch

public class Runtest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Test();
    }
    
    public static int Test()
    {
        int x = 5;
        try {
            int num = x / 0;
            System.out.println(num);
        } catch (ArithmeticException e) {
            
            System.err.println("除数不能为0");
            return 6;
        }
        finally
        {
            ++x;
            System.out.println("finally");
        }
        return 2;
    }
}
Output:
除数不能为0
finally
 

最终结论

    任何执行try 或者catch中的return语句之前,都会先执行finally语句,如果finally存在的话。
如果finally中有return语句,那么程序就return了,所以finally中的return是一定会被return的,
编译器把finally中的return实现为一个warning。
原文地址:https://www.cnblogs.com/xiarongjin/p/8308858.html