Php捕获异常

时间:2011-04-26 19:14:42

标签: php error-handling try-catch

我正在尝试发现错误

        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.标签,我想要捕获,但错误捕获不起作用。

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

您应该阅读有关异常概念的更多信息。以下是一些您可能会觉得有用的链接:

http://www.w3schools.com/php/php_exception.asp

http://ciaweb.net/pear-exception-use-guidelines.html

http://php.net/manual/en/language.exceptions.php

答案 2 :(得分:0)

您确定在try块代码中,错误会引发异常吗? try语句能够捕获此php代码抛出的异常:

throw new Exception('exception raised');

请在“备注”面板中查看here