天天看點

黑馬程式員--異常練習

------------------- android教育訓練、java教育訓練、期待與您交流!----------------

class LanPingException extends Exception//内部抛出,外部得辨別

{

 LanPingException(String Message)

 {

  super(Message);

 }

}

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()

 {

  state = 1;

  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());

   //test();//執行不到,必須寫前面才能執行到

  }

  System.out.println("講課");

 }

 public void test()

 {

  System.out.println("練習");

 }

}

class ExceptionTest

{

 public static void main(String[] args)

 {

  Teacher t = new Teacher("畢老師");

  try

  {

   t.prelect();

  }

  catch (NoPlanException e)

  {

   System.out.println(e.toString());

   System.out.println("換老師或者放假");

  }

 }

}

轉載于:https://www.cnblogs.com/Stone-sw/archive/2013/03/12/2956088.html