I was trying to run the below code snippet in Visual Studio 2010 .
int Number1 = 320000;
int Number2 = 320000;
int Number3 = Number1 * Number2 ;
MessageBox.Show(Number3.ToString());
The code resulted in the value – 797966336 without being showing the error or the correct value .
Just found that If you want an exception to be raised on this occassion , then use the following 2 options as below .
1. Checked Block
Checked
{
int Number1 = 320000;
int Number2 = 320000;
int Number3 = Number1 * Number2 ;
MessageBox.Show(Number3.ToString());
}
2. Enable the checked compiler option in Visual Studio Project Properties
To set the compiler option in the Visual Studio 2010 .
1. Right Click on the Project and open Properties.
2. Click the Build Property Tab page
3. Click the Advanced Button near the Output.

4.Check the Property “Check for Arithmetic Overflow/Underflow” and Click ok .
Now run the same code again , you should be able to raise an exception
OverFlow Exception was unhandled .
Arithmetic operation resulted in an overflow.





1 comment
Tweets that mention why doesn't this cause an Exception ? | Ginktage -- Topsy.com
[...] This post was mentioned on Twitter by progg and ru_webdev, Senthil Kumar. Senthil Kumar said: Why doesn’t this cause an Exception ? http://j.mp/eoAPR1 #visualstudio #csharp [...]
Jan 19, 2011