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?
You can use the following steps to enable Arithmetic Overflow/Underflow checking in Visual Studio :
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:
With Arithmetic Overflow/Underflow enabled:
Using a checked block:
The documentation for checked is available here. (Thanks to Sasha for reminding me about it.)