STRANGE behaviour when working with exceptions

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.ToString(), j.ToString(),

result.ToString());
}
catch (Exception* pExc)
{
Console::WriteLine("Exception caught : '{0}' ", pExc->Message);
}

can anybody explain this ?

thnx
Chris
 
C

Carl Daniel [VC++ MVP]

Chris said:
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.ToString(),
j.ToString(),

result.ToString());
}
catch (Exception* pExc)
{
Console::WriteLine("Exception caught : '{0}' ", pExc->Message);
}

can anybody explain this ?

Floating point exceptions are masked by default. When you divide a double
by o you get back positive or negative infinity. The 'int' type has no
value to represent infinity, so there's no sensible default behavior except
to raise an exception.

From native C++ you can enable floating point exceptions using _controlfp().
There does not appear to be any .NET equivalent.

-cd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top