黑马程序员技术交流社区
标题:
异常练习
[打印本页]
作者:
kemeng
时间:
2015-3-13 16:46
标题:
异常练习
/*
毕老师用电脑上课
电脑出现问题:
蓝屏
冒烟
*/
class LanpingException extends Exception
{
LanpingException(String s)
{
super(s);
}
}
class MaoyanException extends Exception
{
MaoyanException(String message)
{
super(message);
}
}
class NoplanException extends Exception
{
NoplanException(String message)
{
super(message);
}
}
class Computer
{
private int state=3;
public void run() throws LanpingException,MaoyanException
{
if(state==2)
throw new LanpingException("电脑蓝屏了");
if(state==3)
throw new MaoyanException("电脑冒烟了");
System.out.println("电脑运行");
}
public void reset()
{
System.out.println("电脑重启");
}
}
class Teacher
{
private String name;
private Computer cmpt;
Teacher(String name)
{
this.name=name;
cmpt=new Computer();
}
public void prelect() 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 Demo6
{
public static void main(String[] args)
{
Teacher t=new Teacher("毕老师");
try
{
t.prelect();
}
catch (NoplanException e)
{
System.out.println(e.toString());
System.out.println("换老师或者放假");
}
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2