关于try catch final 还有直接扔出的区别

public class ZeroTest 
{
    public static void main(String[] args) 
    {
        try
        {
            int i = 100 / 0;
            System.out.print(i);
        }
        catch(Exception e)
        {
            System.out.print(1);
            throw new RuntimeException();
        }
        finally
        {
            System.out.print(2);
        }
        System.out.print(3);
   }
}

输出结果为:12

解析:

1、int i = 100/ 0; 会出现异常,会抛出异常,System.out.print(i)不会执行,
2、catch捕捉异常,继续执行System.out.print(1);
3、当执行 thrownewRuntimeException();  又会抛出异常,这时,除了会执行finally中的代码,其他地方的代码都不会执行
深度思考:

还是需要理解Try...catch...finally与直接throw的区别:try catch是直接处理,处理完成之后程序继续往下执行,throw则是将异常抛给它的上一级处理,程序便不往下执行了。本题的catch语句块里面,打印完1之后,又抛出了一个RuntimeException,程序并没有处理它,而是直接抛出,因此执行完finally语句块之后,程序终止了。

 
原文地址:https://www.cnblogs.com/sisi-job/p/8613016.html