try...catch的方式处理多个异常

/**
         * try...catch的方式处理多个异常
         * JDK7以后处理多个异常的方式及注意事项
         *
         * 安卓,客户端开发,如何处理异常?try{}catch(Exception e){}
         * ee,服务端开发,一般都是底层开发,从底层向上抛
         *
         * try后面如果跟多个catch,那么小的异常放前面,大的异常放后面,根据多态的原理,如果大的放前面,就会将所有的子类对象接收
         * 后面的catch就没有意义了
         */
        //demo1();
        int a = 10;
        int b = 0;
        int[] arr = { 11, 22, 33, 44, 55 };
        //JDK7如何处理多个异常
        try {
            System.out.println(a / b);
            System.out.println(arr[10]);
        }
             catch (ArithmeticException | ArrayIndexOutOfBoundsException e) {
                System.out.println("出错了");
              };
        System.out.println("out");
    }
    

    public static void demo1() {
        int a = 10;
        int b = 0;
        int[] arr = { 11, 22, 33, 44, 55 };
        try {
            System.out.println(a / b);
            System.out.println(arr[10]);
            arr = null;
            System.out.println(arr[1]);
        }
             catch (ArithmeticException e) { System.out.println("除数为0");
              }catch (ArrayIndexOutOfBoundsException e) {
              System.out.println("索引越界"); }
             catch (Exception e) {
            System.out.println("出错了");
        }
        ;
        System.out.println("out");
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值