Looks like a very easy error to me.
You can't calculate Strings with numbers.
That would have been a run-time error, and in fact, I don't think it would cause a problem.
The issue is actually caused by a missing ; on the "addition failed" echo line.
<?php
$a = 5;
$b = "word";
try
{
$c = $a+$b;
}
catch (Exception $e)
{
echo "Message: addition failed.";
}
echo $c;
?>
it doesn't trigger the exception, but it only shows 5; since + is only defined for numbers. You'd expect that to raise an exception, but it doesn't. In fact, it doesn't even cause an error.
Even as it is now, the try...catch won't catch runtime exceptions, like division by zero; because these are (for some reason) handled by the interpreter itself; in fact, the only exceptions you can Catch with a Try block are the ones you Throw yourself.
You can cause errors to be handled as exceptions if you define your own error handler routine:
function errorHandler($number, $string, $file = 'Unknown', $line = 0, $context = array())
{
if (($number == E_NOTICE) || ($number == E_STRICT))
return false;
if (!error_reporting())
return false;
throw new Exception($string, $number);
return true;
}
set_error_handler('errorHandler');
It still won't catch the issue with + and strings; although I suppose that is more the domain of a warning. It will catch things like division by zero and file not found errors, though.
EDIT: is it just me, or is my first code tag syntax highlighted?