Why doesn’t this cause an Exception ?

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.


If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

No Responses

Leave a Reply


x

Interesting BlogPosts to read

What is Google's Project Loon?
Is Internet currently available for every one? It seems that two third of the world's population doe...
List of Files Created by a new Windows 8 Project
When we create a new Windows 8 (Modern UI) style App, the following files are created by default...
How to block Ads in Internet Explorer 9 ?
If you are one of those users who wants to block Ads in Internet Explorer 9 , you can do it easily w...