捕获PHP中的异常语法错误

wai*_*933 0 php exception-handling exception throw

我试图在PHP中使用异常作为避免多个if-then-else块的方法.但是,当我尝试捕获异常时,我收到错误Parse error: syntax error, unexpected T_CATCH in /directory/functions.php on line 66.我的投掷和捕捉做错了吗?

function doThis($sSchool, $sDivision, $sClass, $sUsername, $sCode,$query1,$query2) 
    {
        connectDb();
        global $dbConnection;

        $sDivisionIdArray = mysqli_query($dbConnection,$query1);
        if ($sDivisionIdArray==false){throw new Exception ();}


        $sDisplayQueryArray = mysqli_query($dbConnection,$query2);
        if ($sDisplayQueryArray==false){throw new Exception ();}

    catch (Exception $e) // This is line 666
        {echo ('Sorry, an error was encountered.');}
    }
Run Code Online (Sandbox Code Playgroud)

Dan*_*inu 5

你忘了这个try说法.

function doThis($sSchool, $sDivision, $sClass, $sUsername, $sCode,$query1,$query2) 
{
    try
    {
       connectDb();
       global $dbConnection;

       $sDivisionIdArray = mysqli_query($dbConnection,$query1);
        if ($sDivisionIdArray==false){throw new Exception ();}


       $sDisplayQueryArray = mysqli_query($dbConnection,$query2);
       if ($sDisplayQueryArray==false){throw new Exception ();}
    }
    catch (Exception $e) // This is line 666
    {echo ('Sorry, an error was encountered.');}
}
Run Code Online (Sandbox Code Playgroud)