//关于异常的小程序
//需求是:老师讲课过程出现的一些问题的处理
//比如说:电脑蓝屏
//电脑死机
class computer//建立电脑类
{
private int sta=3;
public void run()throws lanpingException,maoyanExdception
{
if(sta==2)
throw new lanpingException("电脑蓝屏");
if(sta==3)
throw new maoyanException("电脑冒烟");
System.out.print("电脑运行!");
}
public void rest()
{
System.out.print("重启!");
}
}
class teacher//建立老师类
{
private String name;
private computer comp;
teacher(String name)
{
this.name=name;
comp=new computer();
}
public void jiang()throws wentiException
{
try
{
comp.run();
}
catch(lanpingException l)
{
comp.rest();
System.out.print("开始上课");//为什么这儿不能用comp.run();???????????????????
}
catch(maoyanException m)
{
test();
throw new wenTi("课时无法进行");
}
}
public void test()
{
System.out.println("联系");
}
}
class maoyanException extends Exception//建立问题类
{
maoyan(String msg)
{
super(msg);
}
}
class wenti extends Exception
{
wenti(String msg)
{
super(msg);
}
}
class lanping extends Exception//建立问题类
{
lanping(String msg)
{
super(msg);
}
}
public class YichangDemo
{
public static void main(String[] args)
{
teacher t=new teacher("王五");
try
{
t.jiang();
}
catch(wentiException w)
{
System.out.println(w.toString());
System.out.print("换老师或放假");
}
}
}
|
|