异常的处理的简单的小实验

版权声明: https://blog.csdn.net/yz18931904/article/details/82864431

说明:抛的是自子类的异常  但方法上面声明的父类的异常,其他的方法在 catch的时候,依然会优先捕获子类的异常,也就是catch方法会以实际的产生的异常为准。不会以throws里面的方法为准

public class test2 {
    
    public int meth() throws Exception  {
        
        throw new BusinessException("asddfs");
        
    }

}

public class test {

    public static void main(String[] args) {

        test2 test = new test2();
        int div;

        try {
            div = test.meth();
        } catch (BusinessException e) {

            System.out.println(2);
        } catch (Exception e) {

            System.out.println(1);
        }

    }

}

猜你喜欢

转载自blog.csdn.net/yz18931904/article/details/82864431
今日推荐