加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 致命错误:没有try / catch块的未捕获异常

发布时间:2020-12-13 17:14:01 所属栏目:PHP教程 来源:网络整理
导读:我正在尝试在表单字段为空时以及插入查询不成功时抛出异常.我已经看到有人在不使用try / catch块而不包含Exceptions类之前抛出异常.有谁知道我会怎么做? 当我没有填写所有字段时,这是我得到的错误: 致命错误:未捕获的异常“异常”,消息为“错误:以下字段
我正在尝试在表单字段为空时以及插入查询不成功时抛出异常.我已经看到有人在不使用try / catch块而不包含Exceptions类之前抛出异常.有谁知道我会怎么做?

当我没有填写所有字段时,这是我得到的错误:

致命错误:未捕获的异常“异常”,消息为“错误:以下字段为空 – 标题,电话号码,电子邮件,”在第94行的/vagrant/web/Assignment4/Person.php中例外:错误:以下字段为空 – 标题,在第94行的/vagrant/web/Assignment4/Person.php中调用堆栈:0.0014 638168 1. {main}()/ vagrant/web/Assignment4/Form.php:0 0.0172 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;
               }      
   */
        }

   }

解决方法

你不能在没有“尝试”某事的情况下抛出一个捕捉错误;

$errorArray = array();   

$expectedValues = array(
    "firstName"   => "First Name",);

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[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块的未捕获的异常”.代码捕获了一个错误,但它并没有真正抓住任何东西.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读