PHP Exception Handling - ErrorException

PHP Exception Handling – DivisionByZeroError

In today’s article we’ll continue our travels through the PHP Exception Handling series with a closer look at the DivisionByZeroError. As you might suspect, the DivisionByZeroError is (sometimes) thrown when attempting to divide a value by zero. The challenge is the caveat of “sometimes”, because this particular error might not be thrown as often as you think.

We’ll start by looking at where the DivisionByZeroError sits in the PHP Exception Hierarchy, before moving on to some functional code samples that illustrate when DivisionByZeroErrors will (and won’t) be thrown, so let’s get going!

The Technical Rundown

When Should You Use It?

Most of us are familiar with the trouble that comes when trying to divide by zero. From playing with our calculator back in school days to modern programming and development, the fact remains that attempting to divide by zero is always considered undefined. Since it does us no good to dwell any further on why that is the case, let’s jump right into our code sample and see how DivisionByZeroErrors are commonly thrown in the realm of PHP, and what we can (and cannot) do to handle them:

Let’s begin with our modulo() function, which accepts two parameters and performs a division of the two before spitting out any remainder:

To test these out we’ve made two calls to modulo(): The first using 10 and 3, while the second uses 10 and 0:

The log output shows us that the first invocation works just fine, while the second call throws a DivisionByZeroError, which we were able to successfully catch as intended:

Now let’s try our divide() function, which behaves much the same way, but attempts a straight division instead of a modulo operation:

This time we’ll pass in 10 and 2 the first time, along with 10 and 0 again the second call:

Again, the first call behaves as expected and outputs our result, but the second call is a little bewildering. Even though we clearly performed a division operation using a divisor of zero, no DivisionByZeroError is thrown. Instead, the $result of the calculation merely returns as INF:

Strange, what gives? Keen observers may have noticed we explicitly set the error reporting level at the top of our script to only include the E_ERROR flag, which is described in the documentation as including only “Fatal run-time errors.” As it turns out, PHP doesn’t consider straight up division by zero, as we saw in our second divide() call above, to be a fatal run-time error. Instead, it considers it a warning, which can be seen if we include the E_WARNING flag in our error_reporting() function call at the top:

Now, when we rerun our two divide() calls we still get the same INF $result of the second call, but we also see a PHP Warning indicating that there was an attempt to divide by zero:

The last thing we’ll try is calling the built-in intdiv() function by using our performIntDiv() wrapper function:

As it happens, intdiv() behaves much the same as a modulo, meaning it will also throw an actual DivisionByZeroError when a value of zero is provided as the second argument, as shown by the output from above:

Check out the Airbrake-PHP library, designed to quickly and easily integrate into any PHP project, giving you and your team access to real-time error monitoring and reporting throughout your application’s entire life cycle. With automatic, instantaneous error and exception notifications at your fingertips, you’ll be constantly aware of your application’s health, including any issues that may arise. Best of all, with Airbrake’s robust web dashboard cataloging every error that occurs, you and your team can immediately dive into the exact details of what went wrong, making it easy to quickly recognize and resolve problems.