我想在表單域爲空並且插入查詢不成功時引發異常。我看到有人在不使用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;
}
*/
}
}
啊,非常有幫助。我的眼睛被打開了。謝謝你,先生! –
很高興幫助。 upvote(向上箭頭回答)未來的人。 –