多异常处理

package Exception;
/*
 * 多异常处理
 * 
 * 1.在声明异常的时候,建议声明更为具体的异常,这样异常可以得到更具体的处理
 * 
 * 注意:函数当中只要有异常发生,该函数就已经结束了,所以在多个catch的情况下,只可能执行一个catch里面的处理代码
 */
public class throwsExceptionS {
	public static void main(String[] args) {
		try{
			div(4,0);
		}catch(ArithmeticException ae){			//处理算术异常
			System.out.println("算术异常。。。");
			System.out.println(ae.toString());
		}catch(ArrayIndexOutOfBoundsException ao){	//处理角标越界异常
			System.out.println("角标异常。。。");
			System.out.println(ao.toString());	
		}
	}

	public static int div(int a, int b) throws ArithmeticException,ArrayIndexOutOfBoundsException{
		
		int[] arr = new int[a];	//创建一个数组,长度为传递过来的a数据
		System.out.println(arr[5]);	//这里一定会发生数组角标越界异常
		
		return a / b;	//这里有可能会发生算术异常
	}
}

  

posted on 2013-07-27 19:59  樱花满天  阅读(164)  评论(0)    收藏  举报

导航