strange behaviour when working with exceptions

  • Thread starter Thread starter Chris
  • Start date Start date
C

Chris

Hi,

a strange behaviour when working with exceptions :

when I divide and integer by 0 will an exception be thrown. OK
but, when I divide a double by 0 is no exception thrown ???
How come ?

Try it out.

try
{
int i=1, j=0;
int result = i / j;

// double i=1.0, j=0;
// double result = i / j;

Console.WriteLine("Result {0} / {1} = {2}", i, j,result);
}
catch (Exception pExc)
{
Console.WriteLine("Exception caught : '{0}' ", pExc.Message);
}

can anybody explain this ?

thnx
Chris
 
Chris,

This is right. The double type can represent infinity, positive or
negative, which is what you get when you divide by zero.

Hope this helps.
 
Hi Chris,

So here what the docs say

"Dividing a floating-point value by zero will result in either positive
infinity, negative infinity, or Not-a-Number (NaN) according to the rules of
IEEE 754 arithmetic. Floating-point operations never throw an exception. For
more information, see Single and Double."

-and-

"If a floating-point operation is invalid, the result of the operation is
NaN."

Why? You probably can find the answer in IEEE specs.
 
Back
Top