异常处理

来源:互联网 发布:制作推文用什么软件 编辑:程序博客网 时间:2024/04/30 13:47
//蓝屏异常class LanpingException extends Exception{LanpingException(String msg){super(msg);}}//冒烟异常class MaoyanException extends Exception{MaoyanException(String msg){super(msg);}}//不能继续讲课异常class StopTeachException extends Exception{  StopTeachException(String msg){super(msg);}}//演示class Demo{public static void main(String[] args) {   Teacher t =new Teacher("Zhang");   try   {t.prelect();   }   catch (StopTeachException e)   {   System.out.println("停课");   }  }}//电脑类class Computer{private int state=2;void run() throws LanpingException,MaoyanException{if(state==2)throw new LanpingException("蓝屏了");else if(state==3)throw new MaoyanException("冒烟了");System.out.println("Run");}void reset(){System.out.println("Reset");}}//老师类class Teacher{private String name;private  Computer computer;Teacher(String name){this.name=name;computer=new Computer();}void prelect()  throws StopTeachException{try{computer.run();}catch (LanpingException e){computer.reset();System.out.println("讲课");}catch(MaoyanException e){throw new StopTeachException("不能继续讲课");}}}

0 0