Fatal error: thrown exception without try / catch block

I try to make an exception when the form field is empty, and also when the insert request was unsuccessful. I have seen someone throw exceptions before, without using try / catch blocks and without including the Exceptions class. Does anyone know how I will do this?

This is the error I get when I do not fill out all the fields:

Fatal error: throw an exception "Exception" with the message "Error: the following fields are empty: name, phone number, email" in / vagrant / web / Assignment 4 / Person.php on line 94 Exception: error: the following: the fields are empty. Header, phone number, email, in / vagrant / web / Assignment 4 / Person.php on line 94. Call stack: 0.0014 638168 1. {main} () / vagrant / web / Assignment4 / Form.php: 0010172 698568 2. Person-> insert () / 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; } */ } } 
+4
source share
1 answer

you cannot throw a catch error without trying something,

 $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(); } 

in your code, you threw an error before you attempted, threw the fatal error "uncaught exception without try / catch block". the code caught an error, but in fact it did not try to catch anything.

+2
source

Source: https://habr.com/ru/post/1442564/


All Articles