Search code examples
phpexceptionthrow

Catching Exceptions Syntax Error in PHP


I'm trying to use exceptions in PHP as a way of avoiding multiple if-then-else blocks. However, when I try to catch the exception, I get the error Parse error: syntax error, unexpected T_CATCH in /directory/functions.php on line 66. Am I doing something wrong with my throwing and catching?

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.');}
    }

Solution

  • You can't use catch without a try.