(一) 常见异常的捕获 // // (1) 编写一个程序,分别生成ArrayIndexOutOfBoundsException类型和ArithmeticException类型的异常。

这道题不交,wdnmd

public class Test6_1_zxj {
//    (一)  常见异常的捕获
//
//    (1)     编写一个程序,分别生成ArrayIndexOutOfBoundsException类型和ArithmeticException类型的异常。
//
//    (2)     改写程序,对ArrayIndexOutOfBoundsException和ArithmeticException类型异常进行捕获。
//
//    思考:能否同时抛出ArrayIndexOutOfBoundsException类型和ArithmeticException类型的异常?为什么?
//答:不能,程序每次只能捕获一个异常,可以把catch全都去掉,会优先报算数异常,捕获算数异常之后,再运行,会报越界异常

    public static void main(String args[])
    {
        int[] a={1,2,3,4};
        int num=0;
        for(int i=0;i<5;i++)
        {
            try {
                num=a[i]/i;
            }
            catch(ArithmeticException e)
            {
                System.out.println("算术异常");
            }
            catch(ArrayIndexOutOfBoundsException e)
            {
                System.out.println("数组越界异常");
            }
            System.out.println(num);
        }
    }
}

在这里插入图片描述
数组越界异常的输出的1,是num的值

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值