Introduction
In this blog, I would like to point out that dividing a number by zero doesn't
always raise an exception as is generally taken for granted. The raising of
exception depends upon the type of the number being divided by zero.
DivideByZeroException
As the name itself suggests, this exception is raised at run time when the
compiler tries to divide a number by zero. But this exception is not raised at
all times. For instance, take the code below:
|
The above piece of code, when executed will raise the DivideByZeroException.
But this is not the case when we divide a number of type double with zero. For instance, if you execute the code below, it will not raise any exception and will just execute normally.
|
Now, this is not a bug but in fact, a very normal scenario. The result of this division will be "Infinity", positive infinity, to be precise. the double type has a property for infinity. This infinity is just like any other number and can also be used in calculations. For instance, the following lines of code can be executed without any compilation errors or run time exceptions.
|
The double type provides methods to check if a number is infinity or not. Some of those properties are:
- double.IsInfinity(double num)
- double.IsPositiveInfinity(double num)
- double.IsNegativeInfinity(double num)
The type also provides properties like:
- double.PositiveInfinity
- double.NegativeInfinity
- double.NaN
This can be used in calculations where infinity is being used.
Please provide your valuable suggestions and comments to improve this tip. I
have also uploaded the source code (.CS file) for a sample application along
with this tip.
Hope this helps!

SubashPosted Sep 15, 2016, 1:19 AM
Nice one