2017-06-14 138 views
-2

所以我儘量處理好與異常相關的錯誤,我有一些問題,所以如果你幫我接力 - PHP例外

所以我有類,這是負責與數據庫連接,那將是完美的

class Db 
{ 


... 


    public function insertPlayer() 
    { 
     try 
     { 
      $this->connect(); 
      $sql = "INSERT INTO Player VALUES();"; 

      if ($this->connection->query($sql) === TRUE) 
      { 
       return true; 
      } 
      else 
      { 
       return false; 
      }   
     } 
     catch(Exception $e) 
     { 
      throw new Exception($e->getMessage()); 
     } 
     finally 
     { 
      $this->disconnect(); 
     } 
    } 
} 

所以基本上,當發生異常的方法拋出新的異常主程序的異常將是處理在較高的水平,因爲更高的水平有錯誤代碼

try 
{ 
    $db_connection = new Db(); 
    if(!$db_connection->isPlayerExist($betslip["player_id"])) 
    { 
     $db_connection->insertPlayer(); 
    } 
    $db_connection->addBetSlip($betslip); 
} 
catch(InsufficientBalanceException $e) 
{ 
    array_push($response["errors"], 11); 
    $response->showErrors($error_codes); 
} 
catch(Exception $e) 
{ 
    array_push($response["errors"], 0); 
    $response->showErrors($error_codes); 
} 

但是,當錯誤發生程序寫在該行,其中在catch塊新的異常拋出是uncatch,雖然會在更高的層面上處理

對不起,對於英文不好

回答

0

您應該使用$e->getCode()捕獲錯誤代碼

catch(Exception $e) 
{ 
    array_push($response["errors"], 0); 
    $response->showErrors($e->getCode()); 
} 
+0

問題是,PHP顯示錯誤,當我嘗試從catch塊從 –

+1

類拋出的異常可以共享錯誤,請 –