PHP異常轉移

/**
PHP之異常轉移
分析:
 老師用電腦上課
   問題領域涉及三個對象
   學校 ,老師,電腦

***/
//電腦異常
class ComputerLanPiException  extends   Exception {
         
         Public   function  __Construct($msg) {
                       parent ::  __Construct($msg);
         }

}
//電腦異常
class ComputerMaoYanException  extends   Exception {
         
         Public   function  __Construct($msg) {
                      parent ::  __Construct($msg);
         }

}



/***學校問題主要管理學校的課程進度的問題異常**/
class   NoPlanException   extends  Exception {
         
        Public   function  __Construct($msg) {
                      parent ::  __Construct($msg);
         }
 
}

Class  Computer {
      private   $state = 2;  //電腦 狀態

      Public  function  run() {  //若是是java的話那麼 他會對run函數進行總體拋出  throw  xx ,xx
            
          if($this->state == 1) {          
               
                throw   new ComputerLanPiException('電腦藍屏了');
          
          }

          if($this->state ==2) {

                throw   new  ComputerMaoYanException('電腦冒煙了');
          
          }

          echo  '電腦運行了...<br/>';
      }
         
         //重啓電腦
      Public  function  reset() {
            echo  '電腦重啓了<br/>';
            $this->state = 0 ;
      }

}

Class   Teacher {

     Private   $names;  //老師的名字
     Private   $comp;  //老師的電腦


     Public   function  __Construct($name,$comp) {
                   $this->names = $name;
                   $this->comp = $comp;
     }

     Public   function   Prelect () {
           try{
             
                      $this->comp->run();   //先運行電腦
                       echo $this->names.'講課者';  //老師名字 

                     } catch (ComputerLanPiException  $e) {  //若是出現藍屏的異常咱們就直接重啓
                
                        $this->comp->reset();   //重啓
                        $this->Prelect() ;  //開始講課對吧
                        echo  '<hr/>';
                        echo  $e->__toString(); //把對象轉換爲字符串 

                      } catch (ComputerMaoYanException  $e){  //電腦壞了怎麼辦法
                         
                           echo  '電腦冒煙了..<br/>';
                          $this->test();            

                           /****************異常轉移*********************/
                          //echo  '<hr/>';    //異常轉換
                          //throw  $e;   //繼續向上拋出異常 這樣拋不合理 由於 學習這塊也沒辦法去解決這狀況電腦換了的狀況

                           //echo  $e->__toString(); //把對象轉換爲字符串 
                          //java中直接 throw  e;
                          //throw  new  ComputerMaoYanException('電腦冒煙了');  //繼續拋出異常
                           $msg ='課程沒法完成  緣由:'.$e->__toString();  //把對象轉換成字符串
                            //異常轉換
                            throw  new   NoPlanException ($msg);  //拋出課程進度沒法完成的異常//這叫異常轉換
                        /****************異常轉移*********************/
                   }
                  
     }

     Private  function   test() {
     
                 echo  '作你們練習';

     }



}



class  ExceptionsTest {
 
      /**自定義主函數*/
     Public   Static  function  Main(){
                    $comp = new Computer();
                    $t = new Teacher("任老師",$comp);  //初始化老師
                    try{
                          $t->Prelect(); //調用老師講課        
                        
                     } catch(NoPlanException $e) {  //這叫異常轉換
                         echo  '<hr/>';
                          echo  $e->getMessage();
                          echo  '<hr/>';
                          echo ' 學校立刻換人了....';
                      
                      /***下面就是換人的****/
                     $comp = new Computer();
                     $comp->reset();
                     $t = new Teacher("王老師",$comp);  //初始化老師
                     $t->Prelect(); //調用老師講課        
        
                    }
                
            
     }

}

header("Content-Type:text/html;charset=utf-8;");
ExceptionsTest :: Main();  //入口
相關文章
相關標籤/搜索