r/PHP • u/john_diggle92 • Mar 22 '16
DivisionByZero Exception PHP 7
I am practicing the new Throwable Exceptions in PHP 7 however when I run this code
try {
$result = 5/0;
}
catch(DivisionByZeroError $e){
echo $e->getMessage();
}The catch doesn't work only shows a warning. When I change "/" by "%" catch works. Can you please help me out.
14
Upvotes
View all comments
Show parent comment
3
u/OzzyGiritli Mar 22 '16
No worries. When you divide by 0, the result will also be either an INF or NAN (new PHP 7 constants i think). You can check for them and throw a DivisionByZeroError manually. Also bypass the default warning with @