1.编写能产生ArrayIndexOutOfBoundsException异常的代码,并将其捕获,在控制台上输出异常信息。
public class Arr {
public static void main(String[] args) {
int array [] = {1,2,4,5,};
try {
array[4]= 5;
} catch (ArrayIndexOutOfBoundsException e) { //捕获异常
System.err.println("数组下标越界!");
e.printStackTrace();
e.getStackTrace();
}
}
}
2.修改第一题,使用log4j记录日记,在jbit.log文件中记录产生的异常信息。
import org.apache.log4j.Logger;
public class Arr1 {
static Logger logger = Logger.getLogger(Arr1.class); //添加日记
public static void main(String[] args) {
int array [] = new int [5];
try {
array[5]=6;
} catch (ArrayIndexOutOfBoundsException e) { //捕获
System.err.println("数组下标越界!!");
logger.error(e.getMessage()); //调用日记
e.printStackTrace(); //打印捕获信息
}
}
}