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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© HeiMaYSL 中级黑马   /  2012-5-5 08:03  /  1266 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

class LanPingException extends Exception
{
        LanPingException(String message)
        {
                super(message);    //重写父类的构造方法:Exception(String message)。
        }
}
class MaoYanException extends Exception
{
        MaoYanException(String message)
        {
                super(message);    //重写父类的构造方法:Exception(String message)。
        }
}
class NoPlanException extends Exception
{
        NoPlanException(String msg)
        {
                super(msg);    //重写父类的构造方法:Exception(String msg)。
        }
}


//电脑类的描述
class Computer
{
        private int state=3;  //电脑状态:1,表正常,2,表蓝屏,3,冒烟
        public void run()throws LanPingException,MaoYanException     //这里必须声明异常
        {
                //对state进行判断后,就抛出相应的异常处理。
                if(state==2)
                        throw new LanPingException("电脑蓝屏了!");
                if(state==3)
                        throw new MaoYanException("电脑冒烟了!");

                System.out.println("电脑运行中......");
        }
        public void restart()
        {
                state=1;     //让电脑状态处于正常,再重启。
                System.out.println("此时电脑正常,电脑重启!");
        }

}

class Teacher
{
        private String name;
        private Computer computer;

        Teacher(String name)        //对老师进行初始化。
        {
                this.name=name;
                computer=new Computer();  
        }

        public void prelect()throws NoPlanException
        {
                try
                {
                        computer.run();   //运行时,状态可能异常
                }
                catch (LanPingException e)  //捕获蓝屏
                {
                        computer.restart();
                }
                catch (MaoYanException e)   //捕获冒烟
                {
                        test();
                        throw new NoPlanException("课时无法继续"+e.msg);
                }
                System.out.println("讲课");
        }

        public void test()
        {
                System.out.println("同学们练习");
        }
}


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

2 个回复

倒序浏览
              throw new NoPlanException("课时无法继续"+e.msg);
e.msg 神马意思? 是e.getMessage() 吧

评分

参与人数 1技术分 +1 收起 理由
贠(yun)靖 + 1

查看全部评分

回复 使用道具 举报
是 的,我马虎了,谢了。问题已经解决了。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马