• 面向对象(多异常的声明与处理)


    //copyright©liupengcheng
    //
    http://www.cnblogs.com/liupengcheng

    /**
    * Created by Administrator on 2014/9/28.
    *
    * 多异常的声明与处理
    *
    * 声明异常时要声明具体的异常,并且要有具体的处理方式。
    * 声明两个异常,就要有两个处理方式,当不确定异常的种类时,不要直接草草处理了。
    * 这样不易于排错
    */

    //copyright©liupengcheng
    //
    http://www.cnblogs.com/liupengcheng


    class Demo4
    {
        int div(int a,int b)    throws ArithmeticException,ArrayIndexOutOfBoundsException //声明函数有可能出现异常

                //ArithmeticException               除以零异常
                //ArrayIndexOutOfBoundsException    角标越界异常

        {
            int[] arr = new int [a];
            System.out.println(arr[0]); //当角标为0时,不越界,不为0时,越界
            return a/b;
        }
    }

    //copyright©liupengcheng
    //
    http://www.cnblogs.com/liupengcheng


    public class ExceptionDemo1 {
        public static void main(String [] args)
        {
            Demo4 d = new Demo4();
            try
            {
                int x = d.div(4,0);     //当传入的b为0时,除零错误,不为0时,不错误
                System.out.println("x"+ x);
            }
            catch(ArithmeticException e)
            {
                System.out.println("除零了");
                System.out.println(e.toString());
            }
            catch(ArrayIndexOutOfBoundsException e)
            {
                System.out.println("角标越界了");
                System.out.println(e.toString());
            }

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

    //copyright©liupengcheng
    //
    http://www.cnblogs.com/liupengcheng
  • 相关阅读:
    P2325 [SCOI2005]王室联邦
    P2709 小B的询问
    P4867 Gty的二逼妹子序列
    P4396 [AHOI2013]作业
    CF617E XOR and Favorite Number
    P4462 [CQOI2018]异或序列
    p4434 [COCI2017-2018#2] ​​Usmjeri
    LOJ 117 有源汇有上下界最小流
    P4137 Rmq Problem / mex
    LOJ 116 有源汇有上下界最大流
  • 原文地址:https://www.cnblogs.com/liupengcheng/p/3998544.html
Copyright © 2020-2023  润新知