Division By Zero Doesn’t Always Raise An Exception

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:

int num1 = 150;
int num2 = 0;
int result = num1/num2; 

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.

double num1 = 15.8
int num2 = 0 
double result = num1/num2;  

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.

Link.

Advertisements
By Sriramjithendra Posted in C#.NET

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s