异常

异常
抛出异常时,用printStackTrace()来跟踪异常信息,会打印最“子”的信息,
如果不是很有必要,就Exception就OK了

 1 public   class  Demo  {
 2    public static void main(String args[]) {
 3        int i = 0;
 4        try {
 5            if (i == 0{
 6                throw new LeafException();
 7            }

 8        }
 catch (TreeException e) {
 9            e.printStackTrace();
10        }

11    }

12}

13
14 class  TreeException  extends  Exception {
15    public TreeException () {
16        System.out.println("TreeException()");
17    }

18}

19
20 class  LeafException  extends  TreeException {
21    public LeafException () {
22        System.out.println("LeafException()");
23    }

24}

打印结果:
TreeException()
LeafException()
LeafException
        at Demo.main(Demo.java:6)

你可能感兴趣的:(异常)