Back to questions
JuniorTheoryCommon
What happens when you run this program?
<?php
function safeDiv(int $a, int $b): string
{
try {
return (string) intdiv($a, $b);
} catch (Exception $e) {
return 'caught';
}
}
echo safeDiv(6, 3), ' ', safeDiv(1, 0), "\n";2, then an uncaught DivisionByZeroError: catch (Exception) misses an Error
- Expecting
catch (Exception $e)to catch aDivisionByZeroError: it extendsError, a separate branch ofThrowable, socatch (Error $e)andcatch (Throwable $e)stop it. - Expecting division by zero to print a Warning and go on: since PHP 8 both
/andintdivthrow aDivisionByZeroErrorwhen the divisor is0.
- Which
catchwould stop this error, and where in a program does it belong? - What would
safeDiv(1, 0)return if the function usedfdivinstead ofintdiv?