2017-06-14 126 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

类抛出的异常可以共享错误,请 –