/*需求
畢老師用電腦上課 祈匙。
開始思考上課中出現(xiàn)的問(wèn)題秤茅。
比如問(wèn)題是
電腦藍(lán)屏
電腦冒煙
要對(duì)問(wèn)題進(jìn)行描述璧亚,封裝成對(duì)象
可是當(dāng)冒煙發(fā)生后砰识,導(dǎo)致講課進(jìn)度無(wú)法繼續(xù)
這時(shí)候出現(xiàn)講師問(wèn)題:課時(shí)計(jì)劃無(wú)法完成
*/
class LanPingException extends Exception? ? //異常可以處理块茁,Exception
{
LanPingException(String msg)
{
super(msg);
}
}
class MaoYanException extends Exception? ? //異常不可以處理齿坷,需要停下來(lái),RuntimeException
{
MaoYanException(String msg)
{
super(msg);
}
}
/*
可是當(dāng)冒煙發(fā)生后龟劲,導(dǎo)致講課進(jìn)度無(wú)法繼續(xù)
這時(shí)候出現(xiàn)講師問(wèn)題:課時(shí)計(jì)劃無(wú)法完成
*/
class NoPlanExceptio extends Exception
{
NoPlanExceptio(String msg)
{
super(msg);
}
}
class Computer
{
private int state = 3;
public void run()throws LanPingException,MaoYanException
{
if(state == 2)
throw new LanPingException("藍(lán)屏了");
if(state == 3)
throw new MaoYanException("冒煙了");
System.out.println("電腦運(yùn)行");
}
public void reset()
{
state = 1;
System.out.println("電腦重啟");
}
}
class Teacher
{
private String name;
private Computer com;
Teacher(String name)
{
this.name = name;
com = new Computer();
}
public void prelect()throws NoPlanExceptio
{
try
{
com.run();
}
catch (LanPingException e)
{
com.reset();
}
catch (MaoYanException e)
{
test();
//throw e;? ? //處理不了胃夏,拋出去,但是并不能解決問(wèn)題? ,解決問(wèn)題方案可以新建立問(wèn)題解決方法
throw new NoPlanExceptio("課時(shí)計(jì)劃被拖延"+e.getMessage());? ? //函數(shù)結(jié)束標(biāo)識(shí)昌跌,下面的語(yǔ)句統(tǒng)統(tǒng)運(yùn)行不了
}
System.out.println("講課");
}
public void test()
{
System.out.println("做練習(xí)");
}
}
class ExceptionTest
{
public static void main(String[] args)
{
Teacher t = new Teacher("畢老師");
try
{
t.prelect();
}
catch (NoPlanExceptio e)
{
System.out.println(e.toString());
System.out.println("換電腦或者放假");
}
}
}