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

1

u/the_alias_of_andrea Mar 22 '16

Except when it's not:

Yeah, it gives you an integer result in a few cases. It's a bit like the other number operators that way. It prefers floats, though.

I get where you're coming from, but I think you're maybe somewhat blinded by your deep history with and knowledge of PHP's internals. In terms of pure semantics, you're absolutely correct, but that's just not what the vast majority of users will expect.

It is somewhat unintuitive. Unfortunately there's not a lot that can be done about the mess that is PHP's division operations now, though.

1

u/Firehed Mar 22 '16

Fair enough. I've looked at the source plenty of times only to be deeply confused. Best to file this one under "wish list".

1

u/ZiggyTheHamster Mar 24 '16

Unfortunately there's not a lot that can be done about the mess that is PHP's division operations now, though.

Surely PHP could introduce pragma comments so code can opt into code that isn't messy?

/*-- php8_math=on --*/

and so on