class LanPingException extends Exception
{
LanPingException(){
super();
}
LanPingException(String message){
super(message);
}
}
class MaoYanException extends Exception
{
MaoYanException(){
super();
}
MaoYanException(String message){
super(message);
}
}
class ClassProgressException extends Exception
{
ClassProgressException(){
super();
}
ClassProgressException(String message)
{
super(message);
}
}
class Computer
{
private int state=3;
Computer(){}
public void run()throws LanPingException,MaoYanException
{
if(state==2){
throw new LanPingException("蓝屏了");
}else if(state==3)
throw new MaoYanException("冒烟了");
else
System.out.println("电脑运行");
}
public void reset()
{
System.out.println("电脑重启");
}
}
class Teacher
{
private String name;
private Computer computer;
Teacher(){}
Teacher(String name){
this.name=name;
computer=new Computer();
}
public String getName(){
return name;
}
//上课
public void teach()throws ClassProgressException
{
try{
System.out.println(name+"上课");
computer.run();
}catch(LanPingException e){
System.out.println(e.getMessage());
computer.reset();
System.out.println("继续上课");
}catch(MaoYanException ee){
System.out.println(ee.getMessage());
throw new ClassProgressException("上课进度异常");
}
}
}
class Demo1
{
public static void main(String[] args)
{
//1.老师用电脑上课,用面向对象的思想实现
//可能发生的异常有电脑蓝屏,电脑冒烟,电脑冒烟会影响讲课进度,引发讲课进度异常.
try{
Teacher t=new Teacher(“杨老师”);
t.teach();
}catch(ClassProgressException e){
System.out.println(e.getMessage());
System.out.println("老师休息,学生练习");
}
}
}