关于异常的一个注意点

关于异常的一个注意点
 1         DataInputStream dis  =   null ;
 2          try {
 3            if(true){
 4                throw new IllegalArgumentException("IllegalArgumentException is throw in here");
 5            }
else{
 6                dis = new DataInputStream(new FileInputStream("C:\\test"));                
 7            }

 8        }
catch (NullPointerException e) {
 9            System.out.println(e);
10        }
finally {
11            dis.close();
12        }
这只是一段测试代码,代码的本意是想抛出一个IllegalArgumentExceptioni,但是最后却抛出了一个空指针异常,主要是因为在finally中没有判断dis是否为空,这样的话就把原来的异常给掩盖了,在LOG的输出中给程序的DEBUG带来了困难,造成了一种假象是空指针的错误.在编码的时候我们注意一下这个问题.

你可能感兴趣的:(关于异常的一个注意点)