题目概述:老师教学,依靠电脑,处理蓝屏、冒烟等异常。
流程图
/*
毕老师讲课
电脑运行
电脑重启
异常情况:
电脑蓝屏
电脑冒烟:处理不了,转换成老师的异常:无法继续讲课
*/
class LanPingException extends Exception
{
LanPingException(String msg)
{
//System.out.println("电脑蓝屏");
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= 3;// Step1: 依次1,2,3赋值进行测试
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()
{
state = 1;
System.out.println("电脑重启");
}
}
class Teacher
{
public String name;
public 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 ExceptionTest
{
public static void main(String[] args)
{
Teacher t=new Teacher("毕老师");
try
{
t.teach();
}
catch(NoPlanException e)
{
System.out.println(e.toString());
System.out.println("放假");
}
}
}