面向对象程序设计——第七章异常,课后作业

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

package come.diqizhang.dome;
/**
 * 
 * @author 数组类
 *
 */
public class ArrayException {
	
	
	public static void main(String[] args) {
		
		int[] score = new int[4]; //创建数组
		
		try {
			
			score[4] = 5;
			
		}catch (ArrayIndexOutOfBoundsException e) { //判断是否数组越界
			
			// TODO: handle exception
			
			System.err.println("数组越界!");
			
			e.getMessage();
			
			e.printStackTrace();
		}
		
	}
}

2.修改第一题,使用log4j记录日志,在jbit.log文件记录产生的异常信息。

package come.diqizhang.dome;


import org.apache.log4j.Logger;

/**
 * 
 * @author Lenovo
 *
 */

public class ArrayException1 {
	
	private static Logger logger =Logger.getLogger(ArrayException1.class.getName());
 
	public static void main(String[] args) {
		// 使用判断数组下标越界的异常方法进行异常处理,并使用log4j记录日志

		int[] score = new int[4];
		
		try {
		
			score[4] = 5;
			
		}catch (ArrayIndexOutOfBoundsException e) {
			
			// TODO: handle exception
			
			System.err.println("数组越界!");
			
			logger.error(e.getMessage());
			
			e.printStackTrace();
			
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值