java 19 - 3 JDK7针对多个异常的处理方式

 1 /*
 2   JDK7出现了一个新的异常处理方案:
 3           try{
 4   
 5           }catch(异常名1 | 异常名2 | ...  变量 ) {
 6               ...
 7           }
 8    如果编译期异常,又不知道异常名,就跟进看源码,那里面就能找到
 9     注意:这个方法虽然简洁,但是也不够好。
10              A:处理方式是一致的。(实际开发中,好多时候可能就是针对同类型的问题,给出同一个处理)
11              B:多个异常间必须是平级关系。
12  */
13 public class ExceptionDemo3 {
14     public static void main(String[] args) {
15         method();
16     }
17 
18     public static void method() {
19         int a = 10;
20         int b = 0;
21         int[] arr = { 1, 2, 3 };
22 
23         // try {
24         // System.out.println(a / b);
25         // System.out.println(arr[3]);
26         // System.out.println("这里出现了一个异常,你不太清楚是谁,该怎么办呢?");
27         // } catch (ArithmeticException e) {
28         // System.out.println("除数不能为0");
29         // } catch (ArrayIndexOutOfBoundsException e) {
30         // System.out.println("你访问了不该的访问的索引");
31         // } catch (Exception e) {
32         // System.out.println("出问题了");
33         // }
34 
35         // JDK7的处理方案
36         try {
37             System.out.println(a / b);
38             System.out.println(arr[3]);
39         } catch (ArithmeticException | ArrayIndexOutOfBoundsException e) {
40             System.out.println("出问题了");
41         }
42 
43         System.out.println("over");
44     }
45 
46 }

 

转载于:https://www.cnblogs.com/LZL-student/p/5914490.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值