Why not numeric overflows?

  • Thread starter Thread starter Raj
  • Start date Start date
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
 
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...
 
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...
 
Yes, once I check the option it throws me the exception!

Without using IDE, how to set this option?

Thank you

Regards
Raj
 
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.
 
Back
Top