2012-10-28 91 views
4

我想在表单域为空并且插入查询不成功时引发异常。我看到有人在不使用try/catch块的情况下抛出异常,并且没有包含Exceptions类。有谁知道我会怎么做呢?致命错误:未尝试/ catch块未捕获的异常

这是我收到的时候我不填写所有字段的错误:与消息“错误未捕获的异常“例外”:

致命错误以下字段标题空阱,电话号码,电子邮件, 'in /vagrant/web/Assignment4/Person.php on line 94例外:错误:以下字段为空 - 标题,电话号码,电子邮件,位于94行的/vagrant/web/Assignment4/Person.php调用堆栈:0.0014 638168 1. {主}()/vagrant/web/Assignment4/Form.php:0 0.0172 698568 2.人 - >插入()/vagrant/web/Assignment4/Form.php:179

public function insert() 
{ 

    //Storing required $_POST array fields in variable for isset function  
    $errorArray = array(); 

    $expectedValues = array(
     "firstName" => "First Name", 
     "lastName" => "Last Name", 
     "title"  => "Title", 
     "office"  => "Office", 
     "phoneNumber" => "Phone Number", 
     "email"  => "Email", 
     ); 


    //Checking to see if all fields are filled in 

    foreach ($expectedValues as $field => $humanName) { 
     if (empty($_POST[$field])) { 
     $errorArray[] = $humanName . ", "; 
     } 
    } 

    if (count($errorArray) > 0) { 
      throw new Exception("Error: The following fields are empty- " .implode(' ', $errorArray)); 
     } 


    else{ 


     //If they are, insert them into the Perosn table  

       $insert = $this-> doQuery("INSERT INTO Person 
             VALUES(
             '$_POST[firstName]', 
             '$_POST[lastName]', 
             '$_POST[title]', 
             '$_POST[office]', 
             '$_POST[phoneNumber]', 
             '$_POST[email]')"); 



     //If insert query is successful, return true 
      if ($insert === true){ 
       echo "<h2>" . "Congragulations! You now work for Assignment 4 Incorporated" . "</h2>"; 
       return true; 
      } 


     //If not, throw an exception  

      /* else{ 
       throw new Exception 
       ("<p>" . "Error: Query was unsuccessful:" . " " . $this->error . "</p>"); 
       } 

       try{ 
        $insert == true; 
       } 
       catch (Exception $x){ 
        echo $x->getMessage; 
       }  
    */ 
     } 

    } 

回答

2

你不能抛出一个catch没有“尝试”某些东西的恐怖;

$errorArray = array(); 

$expectedValues = array(
    "firstName" => "First Name", 
    "lastName" => "Last Name", 
    "title"  => "Title", 
    "office"  => "Office", 
    "phoneNumber" => "Phone Number", 
    "email"  => "Email", 
); 

try{ 
    foreach ($expectedValues as $field => $humanName) { 
     if (empty($_POST[$field])) { 
      $errorArray[] = $humanName . ", "; 
     } 
    } 

    if (count($errorArray) > 0) { 
     throw new Exception("Error: The following fields are empty- " .implode(' ', $errorArray)); 
    }else{ 
     $insert = $this-> doQuery("INSERT INTO Person 
      VALUES(
      '$_POST[firstName]', 
      '$_POST[lastName]', 
      '$_POST[title]', 
      '$_POST[office]', 
      '$_POST[phoneNumber]', 
      '$_POST[email]')" 
     ); 

     if ($insert === true){ 
      echo "<h2>" . "Congragulations! You now work for Assignment 4 Incorporated" . "</h2>"; 
      return true; 
     } 
    } 
}catch(Exception $e){ 
    echo $e->getMessage(); 
} 

在您的代码中,您在尝试之前抛出错误,导致致命错误“没有try/catch块的未捕获异常”。该代码发现了一个错误,但它并不是真的想要捕捉任何东西。

+0

啊,非常有帮助。我的眼睛被打开了。谢谢你,先生! –

+0

很高兴帮助。 upvote(向上箭头回答)未来的人。 –

相关问题