• 面向对象(throws和throw的区别)


    /**
    * Created by Administrator on 2014/9/29.
    *
    * throws和throw的区别
    *
    * throws使用在函数上,throw使用在函数内
    *
    * throws后面跟的是异常类,可以跟多个,用逗号连接
    * throw后面跟的是异常对象
    */

    //copyright©liupengcheng
    //
    http://www.cnblogs.com/liupengcheng
    class fushuException extends Exception
    {
        private int value;
        fushuException(String msg,int value)
        {
            super(msg);
            this.value = value;
        }
        public int getValue()
        {
            return value;
        }
    }

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

    class Demo5
    {
        int div(int a,int b)    throws fushuException //fushuException    自定义负数异常
        {
            if(b<0)
                throw new fushuException("出现了除数是负数的情况",b);
            return a/b;
        }
    }

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


    public class ExceptionDemo3 {
        public static void main(String [] args)
        {
            Demo5 d = new Demo5();
            try
            {
                int x = d.div(4,-1);
                System.out.println("x"+ x);
            }
            catch(fushuException e)
            {
                System.out.println(e.toString());
                System.out.println("出现问题的数值是"+ e.getValue());
            }

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

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

  • 相关阅读:
    Sum of Medians
    MINSUB
    Girls Love 233
    How Many Answers Are Wrong
    Sorting It All Out
    Cube Stacking
    Boolean Expressions
    表达式求值
    #1301 : 筑地市场
    用seaborn对数据可视化
  • 原文地址:https://www.cnblogs.com/liupengcheng/p/4000237.html
Copyright © 2020-2023  润新知