zoukankan      html  css  js  c++  java
  • 面向对象(多异常的声明与处理)

    //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
  • 相关阅读:
    Hihocoder-小Hi的烦恼
    Python包下载与离线安装
    Shell输出颜色设置
    MySQL主从配置
    MySQL初始化与用户配置
    [转]常用 GDB 命令中文速览
    搭建github静态博客
    树莓派上手
    vim安装与配置
    数组,看了你就懂了!
  • 原文地址:https://www.cnblogs.com/liupengcheng/p/3998544.html
Copyright © 2011-2022 走看看