您的位置:首页 > 编程语言 > Java开发

JavaSE8基础 try...catch(多个异常) 多个异常采取同样的解决措施

2017-09-20 15:38 369 查看
礼悟:
公恒学思合行悟,尊师重道存感恩。叶见寻根三返一,江河湖海同一体。
虚怀若谷良心主,愿行无悔给最苦。读书锻炼养身心,诚劝且行且珍惜。

os :windows7 x64
jdk:jdk-8u131-windows-x64
ide:Eclipse Oxygen Release (4.7.0)


code:

package jizuiku0;

/*
* @version V17.09
*/
public class ExceptionJDK7Demo {
public static void main(String[] args) {

try {
int a = 1 / 0;// 除以0
} catch (ArithmeticException | ArrayIndexOutOfBoundsException e) {
// 多个异常见用 | 隔开
// 多个异常必须是平级关系
System.out.println("发生了ArithmeticException 或者 ArrayIndexOutOfBoundsException 异常");
}

try {
int[] a = { 1, 2 };
System.out.println(a[3]); // 越界
} catch (ArithmeticException | ArrayIndexOutOfBoundsException e) {
// 出现多个异常,采取同样的处理措施
// 多个异常见用 | 隔开
// 多个异常必须是平级关系
System.out.println("发生了ArithmeticException 或者 ArrayIndexOutOfBoundsException 异常");
}
}
}


result:



Java优秀,值得学习。
学习资源:itcast和itheima视频库。如果您有公开的资源,可以分享给我的话,用您的资源学习也可以。
博文是观看视频后,融入思考写成的。博文好,是老师讲得好。博文坏,是 给最苦 没认真。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: