Why not numeric overflows?

R

Raj

int a = int.MaxValue;
int b = int.MaxValue-1;

Console.WriteLine("Before Swapping");
Console.WriteLine("a={0} b={1}", a, b);

try
{
a = a+b;
Console.WriteLine("a+b=", a);
b = a - b;
a = a - b;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}

Console.WriteLine("After Swapping");
Console.WriteLine("a={0} b={1}", a, b);

a=a+b becomes -3, justify!

Why is it not throwing any numeric overflow error?
If i give a=int.MaxValue+1 i get compile time error: overflow, but is the
CLR not bothered about run time overflow? or when will i get runtime numeric
overflow?

Pl. explain
 
P

Patrice

Hi,
Why is it not throwing any numeric overflow error?

Checking overflows at runtime is something you can enable/disable. See
"Project properties", "Build", "Advanced" and check the appropriate
checkbox...
 
R

Raj

I did try that option! no respite!!

Patrice said:
Hi,


Checking overflows at runtime is something you can enable/disable. See
"Project properties", "Build", "Advanced" and check the appropriate
checkbox...
 
R

Raj

Yes, once I check the option it throws me the exception!

Without using IDE, how to set this option?

Thank you

Regards
Raj
 
G

Göran Andersson

Raj said:
Yes, once I check the option it throws me the exception!

Without using IDE, how to set this option?

Thank you

Regards
Raj

As you set the option in the project setting, this will affect how the
project is compiled. Once it's compiled into the code, it doesn't matter
where you run it.
 

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