2

I'm trying to catch an error

        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");
        }

Some data comes with 2 same tag and the others with 3 and I want to catch if there not exist the 3. tag, but the error catching not really work.

2
  • 1
    Could you elaborate on "not really work?" Commented Apr 26, 2011 at 19:16
  • Something has to actually throw an exception for you to be able to catch it.
    – Marc B
    Commented Apr 26, 2011 at 19:35

3 Answers 3

1

You should read a little more about the concept of exceptions. Here are a few links that you might find useful:

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

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

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

1

You can throw your own exception in the try block

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

Are you sure that, in the try block code, an error will throw an exception? The try statment is able to catch the exception which is threw by this php code:

throw new Exception('exception raised');

Please have a look here, in the Note pane.

Not the answer you're looking for? Browse other questions tagged or ask your own question.