第7章课后作业 注:4月19日到5月7号的课后作业是java第二本书面向对象程序设计

1,编写能产生ArrayIndexOutOfBoundsException异常的代码,并将其捕获,在控制台输出异常信息

package bdqn 
 public class Shuzu {  
    static int num[]=new int[3];//数组  
     public static void main(String[] args) {  
     try {//捕获  
         num[3]=4;  
     }catch(ArrayIndexOutOfBoundsException e) {//异常代码块  
         System.err.println("数组过界!!!");  
          e.printStackTrace();  
     }finally {  
         System.out.println("谢谢使用!");  
     }  
     }  
}  
2,修改上一题,使用log4j记录日志,在jbit.log文件中记录产生的异常信息



package bdqn;  
  public class Puhuo {  
    static Logger log=Logger.getLogger(ArrayExceotion.class);//添加日志  
     static int num[]=new int[3];//数字数组  
     public static void main(String[] args) {  
     try {//捕获  
         num[3]=4;  
     }catch(ArrayIndexOutOfBoundsException e) {  
         System.err.println("数组过界!!!");  
           
          log.error( e.getMessage());//调用日志  
     }finally {  
         System.out.println("谢谢使用!");  
     }  
     }  
}  

猜你喜欢

转载自blog.csdn.net/liyiming85/article/details/80231374