I am currently trying to understand the behavior of the new error handling in PHP 7.
In the PHP Documentation for DivisionByZeroError
, it states:
DivisionByZeroError is thrown when an attempt is made to divide a number by zero.
Fair enough, but it does not throw the DivisionByZeroError when the / operator is used.
In this example, I would expect both errors to be caught:
declare(strict_types=1);
function usesIntDiv(int $a, int $b) {
return intdiv($a,$b);
}
function divide(int $a, int $b) {
return $a / $b;
}
try {
echo usesIntDiv(2,0);
} catch (DivisionByZeroError $e) {
echo "Caught DivisionByZeroError!\n";
}
echo "\n";
try {
echo divide(2,0);
} catch (DivisionByZeroError $e) {
echo "Caught DivisionByZeroError!\n";
}
Instead only the first one is caught:
Caught DivisionByZeroError!
PHP Warning: Division by zero in TypeError.php on line 9...
Why? Are there other cases like this? My understanding is that if you catch Throwable
you will catch anything that can be raised, which would make PHP error handling a bit more manageable. But in this case, if I use the /
operator, it is the uncatchable PHP warning.
Is this specific to this error (and maybe because it was triggered by an operator), or am I misunderstanding the change in error handling?