Php捕获异常

Php捕获异常,php,error-handling,try-catch,Php,Error Handling,Try Catch,我正试图抓住一个错误 try { $outcome = $bet->getElementsByTagName("Outcome"); $line1 = $outcome->item(0)->getElementsByTagName("OptionalValue1")->item(0)->nodeValue;

我正试图抓住一个错误

        try     
        {
            $outcome            =   $bet->getElementsByTagName("Outcome");
            $line1              =   $outcome->item(0)->getElementsByTagName("OptionalValue1")->item(0)->nodeValue;                      
            $line2              =   $outcome->item(2)->getElementsByTagName("OptionalValue1")->item(0)->nodeValue;
            $aOdds["line"]      =   ($line1 == 0) ? -$line2 : $line1;

            $aOdds["q1"]        =   $outcome->item(0)->getAttribute("odds"); 
            $aOdds["qx"]        =   $outcome->item(1)->getAttribute("odds"); 
            $aOdds["q2"]        =   $outcome->item(2)->getAttribute("odds");
        }
        catch (Exception $e)
        {
            $outcome            =   $bet->getElementsByTagName("Outcome");
            $line1              =   $outcome->item(0)->getElementsByTagName("OptionalValue1")->item(0)->nodeValue;                      
            $line2              =   $outcome->item(1)->getElementsByTagName("OptionalValue1")->item(0)->nodeValue;
            $aOdds["line"]      =   ($line1 == 0) ? -$line2 : $line1;

            $aOdds["q1"]        =   $outcome->item(0)->getAttribute("odds"); 
            $aOdds["qx"]        =   0; 
            $aOdds["q2"]        =   $outcome->item(1)->getAttribute("odds");
        }

有些数据带有2个相同的标签,而另一些数据带有3,如果不存在3,我想捕获。标记,但错误捕获实际上不起作用。

您可以在try块中抛出自己的异常

if (some condition) {
  throw new Exception("Error message");
}

您确定在try块代码中,错误将引发异常吗? try语句能够捕获由以下php代码引发的异常:

throw new Exception('exception raised');

请在“备注”窗格中查看。

您应该阅读更多有关异常概念的内容。以下是一些您可能会发现有用的链接:


你能详细说明一下“不是真的工作”吗?有些东西必须抛出一个异常,你才能捕捉到它。