Click here to Skip to main content
15,615,774 members
Articles / Visual Studio
Technical Blog
Posted 6 May 2013

Tagged as


3 bookmarked

Enabling Underflow and Overflow Checking within Visual Studio

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
6 May 2013CPOL3 min read
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.

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:

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


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:

     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):

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


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.


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

Written By
Software Developer (Senior)
United States United States
An experienced Software Developer and Graphic Designer with an extensive knowledge of object-oriented programming, software architecture, design methodologies and database design principles. Specializing in Microsoft Technologies and focused on leveraging a strong technical background and a creative skill-set to create meaningful and successful applications.

Well versed in all aspects of the software development life-cycle and passionate about embracing emerging development technologies and standards, building intuitive interfaces and providing clean, maintainable solutions for even the most complex of problems.

Comments and Discussions

QuestionSmall type Pin
Member 1091320529-Jun-14 18:43
Member 1091320529-Jun-14 18:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.