19

I was working around with C# and noticed that when I had a very large integer and attempted to make it larger. Rather that throwing some type of overflow error, it simply set the number to the lowest possible value (-2,147,483,648) I believe.

I was wondering if there was a way to enable the overflow checking in Visual Studio?

josh3736
  • 139,160
  • 33
  • 216
  • 263

1 Answers1

33

You can use the following steps to enable Arithmetic Overflow/Underflow checking in Visual Studio :

  1. Right click on your project in the Solution Explorer and select Properties.
  2. On the Build tab, click the Advanced button. (It's towards the bottom)
  3. Check the "Check for arithmetic overflow / underflow" check-box.

This will throw a System.OverflowException when the overflow occurs rather than it's usual operation of changing the value to a minimum value.

Without Arithmetic Overflow/Underflow enabled:

int test = int.MaxValue;
test++;
//Test should now be equal to -2,147,483,648 (int.MinValue)

With Arithmetic Overflow/Underflow enabled:

int test = int.MaxValue;
test++;
//System.OverflowException thrown

Using a checked block:

checked
{
    int test = int.MaxValue;
    test++;
    //System.OverflowException thrown
}

The documentation for checked is available here. (Thanks to Sasha for reminding me about it.)

Rion Williams
  • 74,820
  • 37
  • 200
  • 327
  • 12
    You can also use the `checked` keyword to wrap a statement or a set of statements so that they are explicitly checked for arithmetic overflow. Setting the project-wide property is a little risky because oftentimes overflow is a fairly reasonable expectation. – Sasha Goldshtein Feb 02 '11 at 18:54
  • 2
    Note that this compiler option only applies when there is no explicit `checked` or `unchecked` context in the code. Also, it will affect the resulting executable, not just debugging in Visual Studio. See here for documentation: http://msdn.microsoft.com/en-us/library/h25wtyxf%28v=VS.100%29.aspx – Justin Feb 02 '11 at 18:55
  • @Rionmonster and @Sasha - I didn't know about any of those two functionalities. Thanks to both. – Øyvind Bråthen Feb 02 '11 at 18:55
  • Good point Sasha - I was just pulling up a short blog on the checked statement :) The link will follow shortly. – Rion Williams Feb 02 '11 at 18:57
  • Wow - Thanks Rionmonster! This was just what I needed! –  Feb 02 '11 at 19:03
  • 5
    Note that you can also use "checked" in an *expression* context if you only want part of an expression to be checked, rather than a whole statement or group of statements. – Eric Lippert Feb 02 '11 at 20:59
  • If my understanding about this compile option is right, its affecting the assembly where the setting is applied. Even if another assembly calls it which is having different setting, it will not be overridden. What about this setting for the system assemblies such as mscorlib.dll and all? Will they throw exception if we pass long value into int method parameter? If I have just a dll, how can I see what is the "Check for overlfow" setting? – Joy George Kunjikkuru Nov 06 '14 at 21:28