本帖最后由 小黑子 于 2014-8-7 15:10 编辑
//需求:老师讲课需要调用电脑,电脑运行,会抛出蓝屏异常
class Computer
{
int state = 2;
void run() throws LanPingException
{
if( state ==2)
throw new LanPingException("电脑蓝屏了");
System.out.println("电脑运行");
}
}
class LanPingException extends Exception
{
String message;
LanPingException(String msg)
{
message = msg;
}
}
class Teacher
{
String name;
Computer cmpt;
Teacher(String name)
{
this.name = name;
Computer cmpt = new Computer();
}
void prelect()
{
try{
cmpt.run(); //第34行
}
catch(LanPingException e)
{
System.out.println(e.toString());
}
System.out.println(name+"@讲课");
}
}
class ExceptionDemo1
{
public static void main(String[] args)
{
Teacher t = new Teacher("zs");
t.prelect(); //第48行
System.out.println("over!");
}
}
/////////////////////////////////////////////////////////////////////////////////
运行结果如下:
Exception in thread "main" java.lang.NullPointerExcepti
at Teacher.prelect(ExceptionDemo1.java:34)
at ExceptionDemo1.main(ExceptionDemo1.java:48)
|
|