【问题标题】:Fatal error: uncaught exception without try/catch block致命错误:没有 try/catch 块的未捕获异常
【发布时间】:2012-10-28 02:07:45
【问题描述】:

我试图在表单字段为空以及插入查询不成功时引发异常。我之前见过有人在不使用 try/catch 块且不包含 Exceptions 类的情况下抛出异常。有谁知道我会怎么做?

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

致命错误:未捕获的异常“异常”,消息“错误:以下字段为空-标题,电话号码,电子邮件,”在 /vagrant/web/Assignment4/Person.php 的第 94 行异常:错误:以下字段为空 - 标题、电话号码、电子邮件,在第 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;
               }      
   */
        }

   }

【问题讨论】:

    标签: php exception uncaught-exception


    【解决方案1】:

    如果不“尝试”某事,就无法抛出 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 块的未捕获异常”。代码捕获了一个错误,但它并没有真正尝试捕获任何东西。

    【讨论】:

    • 啊,非常有帮助。我的眼睛已经睁开了。谢谢你,先生!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多