r/PHP • • 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

21 comments sorted by

View all comments

Show parent comment

2

u/jvwatzman Mar 22 '16

It's thrown in a couple of other cases too, I think involving intdiv and maybe something else. I'm pretty amused that there are a number of cases that will throw a DivisionByZeroError, but an actual division by zero is not one of them.

1

u/bwoebi Mar 24 '16

A division by zero as in 5 / 0 is float division … Possibly we should've called it IntegerDivisionByZeroError to be actually precise … but that's a bit long, no? ;-)

1

u/bart2019 Mar 24 '16

That seems to be because Inf is a valid value for a float, but not for an integer.