Reputation: 10400
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.
Upvotes: 2
Views: 1704
Reputation: 1219
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
Upvotes: 1
Reputation: 2292
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.
Upvotes: 0
Reputation: 4998
You can throw your own exception in the try block
if (some condition) {
throw new Exception("Error message");
}
Upvotes: 1