Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Visual-Studio

Enabling Underflow and Overflow Checking within Visual Studio

5.00/5 (1 vote)
6 May 2013CPOL3 min read 16.6K  
How to enable underflow and overflow checking within Visual Studio

Overflow and underflow aren’t errors that most developers encounter within their daily lives (unless you work with numbers often) and it's likely even less often that you will actually encounter an actual Overflow exception. This is because within Visual Studio, Arithmetic overflows and underflows are disabled by default and when they occur, they simply fail silently and are set to the lowest possible value of the specific datatype.

This blog post will help you get back those lovely yellow screens of death and other exceptions as well as allow to you to actually capture the related errors that occur rather than simply setting the value to a meaningless “lowest value”.

The Problem

You decided to go crazy and take the maximum value of a numerical datatype to the next level and increment it by one and then nothing happened. No explosions. No exceptions. No fun.

C++
var crazy = int.MaxValue;  //2,147,483,647
crazy++;                   //-2,147,483,648

It can often be important to catch situations like these when they happen because throwing an exception might be a much better alternative than multiplying your bank account by an enormous negative value.

The Solution

There are actually two easy methods of dealing with this problem that I will discuss below:

  • Enabling the Arithmetic Overflow and Underflow option within Visual Studio.
  • Using a checked block around your questionable code that could throw exceptions.

Check It (before you wreck it)

Enabling the Arithmetic Overflow / Underflow option within Visual Studio is a piece of cake and just requires taking the following steps:

Image of Arithmetic Overflow

You can easily toggle the Arithmetic Overflow option within the Advanced Build Settings of your Project.
  1. Right-click your Project within the Solution Explorer.
  2. Select Properties.
  3. Select the Build tab along the left.
  4. Select the Advanced… button (it should be at the very bottom)
  5. Within the Advanced Build Settings dialog, check the Check for arithmetic overflow / underflow check-box (unchecked by default).

You can use the previous “crazy code” that I provided earlier to ensure that it is working as intended and throwing an OverflowException:

C++
var crazy = int.MaxValue;  //2,147,483,647
crazy++;                   //OverflowException : Arithmetic operation resulted in an overflow.

OverflowException

This is one of the situations where we actually wanted an exception to occur.

You could now easily add the appropriate logic to handle these exceptions and be on your way to much cleaner and safer code.

checked It Out

Another way to solve this situation is by using the rarely encountered checked keyword, which will likely take you much less time that traversing through the settings within your project and should provide a bit more flexibility in terms of how it can be used.

The checked keyword can be used either as a statement or as an operator – allowing you to narrow the scope of the areas that you want to handle overflow and underflow exceptions through.

As a statement:

C++
checked
{
     var crazy = int.MaxValue; 
     crazy++; //This will throw an overflow exception since it is wrapped within a checked block
}

and as an operator (to just check a single expression):

C++
var crazy = int.MaxValue;
crazy = checked(crazy + 1); //This will throw an overflow exception (using the checked operator)

Overview

If you work with numerical data often and push the limits of the data-types that you are dealing with or just want a few more tools within your belt to help write a bit safer code (or at least capture when an overflow or underflow occurs), then this will be somewhat helpful for you.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)