黑马程序员技术交流社区

标题: 帮忙解决 异常问题不会解决了 [打印本页]

作者: 杨立考    时间: 2012-10-8 23:03
标题: 帮忙解决 异常问题不会解决了
class Demo1
{
public static void main(String[] args)
{
  Teacher t=new Teacher();
  try
  {
   t.teach();
  }
  catch(ChangeZYException e)
  {
   System.out.println(e.getMessage());
   System.out.println("我要疯了");
  }
}
}
class Teacher
{
Computer c=new Computer();
  void teach()
   {
    try
    {
     c.run();
    }
    catch (LanPingException e)
    {
     System.out.println(e.getMessage());
     c.reset();
     this.teach();
    }
    catch (MaoYanException e)
     {
      System.out.println(e.getMessage());
      System.out.println(" 学生自习");
      throw new ChangeZYException(e.getMessage()+" 看着处理吧...");
     }
  
   }
}
class ZGC
{
  void xl(Computer c)
   {
    c.state=0;
    System.out.println("电脑进行了维修");
   }
}
class Computer
{
int state=0;
void run() throws LanPingException,MaoYanException
  {
   if (state==0)
   {
    System.out.println("电脑运行");
   }
   else if (state==1)
   {
    throw new LanPingException("电脑蓝屏");
   }
   else if(state==2)
    {
     throw new MaoYanException("冒烟");
    }

  }
  void reset()
   {
    System.out.println("电脑重新启动");
    state =0;
   }

}
class LanPingException extends Exception
{
public LanPingException(String message)
  {
   super(message);
  }
}
class MaoYanException extends Exception
{
public MaoYanException(String message)
  {
   super(message);
  }
}
class ChangeZYException extends Exception
{
public ChangeZYException(String message)
  {
   super(message);
  }
}

[img=487,106]file:///C:/Users/lenovo/AppData/Roaming/Tencent/Users/793648972/QQ/WinTemp/RichOle/ZS(3NMKGAB%7DG[S1LUJ6W]AY.jpg[/img]


作者: 杨立考    时间: 2012-10-8 23:08
问题截图放不上去   Doc命令提示是:java10:在相应的try语句主体中不能抛出异常ChangeZYException catch(ChangeZYException e)

作者: 袁艳超    时间: 2012-10-8 23:15
Teacher类的 teach方法要抛出ChangeZYException异常的,方法体里throw new ChangeZYException(e.getMessage() + " 看着处理吧..."); 没有内部处理,就要抛出去
void teach() throws ChangeZYException
作者: 林志进    时间: 2012-10-8 23:58
方法里面产生的异常,需要先 try。。。。catch或者throw   才能被其他类调用使用。
直接throw出来异常,调用的类还需要再次处理。





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2