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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© a8851625 中级黑马   /  2014-10-19 00:20  /  1391 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文


class LanPingException extends Exception
{
        LanPingException(String message)
        {
                super(message);
        }
}

class MaoYanException extends Exception
{
        MaoYanException (String message)
        {
                super(message);
        }
}

class NoPlanException extends Exception
{
        NoPlanException(String msg)
        {
                super(msg);
        }
}

class Computer
{
        private int state=3;
        public void run()throws LanPingException,MaoYanException
        {
                if(state==2)
                        throw new LanPingException("蓝屏了");
                if(state==3)
                        throw new MaoYanException("冒烟了");
                System.out.println("run");
        }
        public void reset()
        {
                state=1;
                System.out.println("重启");
        }
}



class Teacher
{
        private String name;

        private Computer cmpt;

        Teacher(String name)
        {
                this.name=name;
                cmpt=new Computer();
        }
        public void prelect()throws MaoYanException
        {
                try
                {
                        cmpt.run();
                       
                }
                catch (LanPingException e)
                {
                        cmpt.reset();
                }
                catch (MaoYanException e)
                {
                        test();
                        throw new NoPlanException("课时无法继续。");//不懂为啥这里会出现必须对其进行捕获或声明以便抛出。?求解答!谢谢
                       
                }
                System.out.println("讲课");
               
        }
        public void test()
        {
                System.out.println("lianxi");
        }
}



class ExceptionTest
{
        public static void main(String[] args)
        {
                Teacher t=new Teacher("毕老师");
                try
                {
                        t.prelect();
                }
                catch (NoPlanException e)
                {
                        System.out.println(e.toString());
                        System.out.println("换老师。");
                }
               
        }
}

评分

参与人数 1技术分 +1 收起 理由
杨佳名 + 1

查看全部评分

2 个回复

倒序浏览
楼主的代码里面prelect方法既然已经在方法体里面已经对抛出的MaoYanException进行了try,catch的捕捉操作,那么在方法名上就不需要再声明抛出了啊,在catch到MaoYanException的时候楼主抛出了NoPlanException对象,而NoPlanException是可检测异常,因此楼主应该在prelect方法名上声明抛出的应该是NoPlanException而不是MaoYanException。
又或者楼主可以让NoPlanException类继承RuntimeException,因为RuntimeException是运行时异常,所以这样就可以不用声明抛出也不用捕捉

评分

参与人数 1技术分 +1 收起 理由
杨佳名 + 1

查看全部评分

回复 使用道具 举报
罗林军 发表于 2014-10-19 01:10
楼主的代码里面prelect方法既然已经在方法体里面已经对抛出的MaoYanException进行了try,catch的捕捉操作, ...

多谢大侠,果然是应该抛出NoPlanException. MaoYanException无法处理应该抛给NoPlanException的。对,其实直接继承RuntimeException会好点,但是跟着毕老师的课来走。所以想搞清楚问题出在哪里
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马