- class LanPingException extends Exception
- {
- LanPingException(String msg)
- {
- super(msg);
- }
- }
- class MaoYanException extends Exception
- {
- MaoYanException(String msg)
- {
- super(msg);
- }
- }
- class NoPlanException extends Exception
- {
- NoPlanException(String msg)
- {
- super(msg);
- }
- }
- class Computer
- {
- private int state=2;
- public void run() throws LanPingException,MaoYanException
- {
- if (state==2)
- throw new LanPingException("电脑出现蓝屏");
- if (state==3)
- throw new MaoYanException("电脑冒烟了");
- System.out.println("电脑正常运行");
- }
- public void reset()
- {
- state=1;
- System.out.println("电脑重启");
- //run();想在这个地方继续调用一次run()方法,打印"电脑正常运行",不知道怎样调用run()方法!!!求解。。。。
- }
- }
- class Teacher
- {
- private String name;
- private Computer cmpt;
- Teacher(String name)
- {
- this.name=name;
- cmpt=new Computer();
- }
- public void teach() throws NoPlanException
- {
- try
- {
- cmpt.run();
- }
- catch (LanPingException e)
- {
- cmpt.reset();
- }
- catch (MaoYanException e)
- {
- test();
- throw new NoPlanException("课时无法继续,"+e.getMessage());
- }
-
- System.out.println("老师上课");
- }
- public void test()
- {
- System.out.println("做练习......");
- }
- }
- class ExceptionDemo
- {
- public static void main(String[] args)
- {
- Teacher t=new Teacher("毕老师");
- try
- {
- t.teach();
- }
- catch (NoPlanException e)
- {
- System.out.println(e.toString());
- System.out.println("换老师或者放假");
- }
-
- }
- }
复制代码
当时毕老师在视频中也出错了,但是没有调试,自试了很多方法,但是也没成功,求大神指点。。。
|
|