IO流_JDK7针对多个异常的处理方案

本文介绍JDK7引入的新型异常处理方案,通过一个示例演示如何使用catch块捕获多种类型的异常,并进行统一处理。这种方式简化了代码结构,但同时也存在一定的局限性。
package cn.itcast_02;

/*
 * JDK7出一了一个新的异常处理方案:
 * 		try{
 * 
 *  	catch(异常名1 |异常名2 | ... 变量){
 *  		...
 *  	}
 *  
 *  	注意:这个方式虽然简洁,但是也不够好。
 *  		A:处理方式是一致的。(在实际开发中,好多时候可能就是针对同类型的问题,给出同一个处理)
 *  		B:多个异常间必须是平级关系。
 */
public class ExceptionDemo3 {
	public static void main(String[] args) {

		method();
	}

	public static void method() {
		int a = 10;
		int b = 0;
		int[] arr = { 1, 2, 3 };

		// try {
		// System.out.println(arr[3]);
		// System.out.println(a / b);
		// } catch (ArithmeticException e) {
		// System.out.println("除数不能为0");
		// } catch (ArrayIndexOutOfBoundsException e) {
		// System.out.println("数据越界");
		// } catch (Exception e) {
		// System.out.println("有问题");
		// }

		// JDK7的处理方案
		 try {
		 System.out.println(arr[3]);
		 System.out.println(a / b);
		 } catch (ArithmeticException | ArrayIndexOutOfBoundsException e) {
		 System.out.println("出问题了");
		 }

		System.out.println("over");
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值