A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© 孙铭泽 中级黑马   /  2012-8-20 22:35  /  1677 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

class MyException extends Exception
{
        private int num;

        MyException(){}

        MyException(String message)
        {
                super(message);
        }

        MyException(String message,int num)
        {
                super(message);
                this.num = num;
        }

        public int getNum()
        {
                return num;
        }
}


class MathDemo
{
        public int divide(int a,int b) throws MyException
        {
                if(b<0)
                {
                        throw new MyException("除数不能为负数",b);
                                        }
                else
                {
                        return a/b;
                }
        }
}

class ExceptionDemo8
{
        public static void main(String[] args)        {
                MathDemo md = new MathDemo();
         //  【这里面的应该怎么写,有点不会了】
                }

评分

参与人数 1技术分 +1 收起 理由
郑文 + 1

查看全部评分

2 个回复

倒序浏览
try
{  
        MathDemo md = new MathDemo();
       double md.divide(5,2);
        System.out.println(result);  
  } catch (MyExceptions e)
{  
         System.out.println(e.message());  
}

评分

参与人数 1技术分 +1 收起 理由
宋天琪 + 1

查看全部评分

回复 使用道具 举报
try
{
    int x=md.divide(8,3);
    System.out.println("x="+x);
}
catch(MyException e)
{
   System.out.println("除数不能为负数");
}
System.out.println("over");

int型的a和b进行a/b运算后定义int型的就行
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马