Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

Enabling Underflow and Overflow Checking within Visual Studio

, 6 May 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Overflow and underflow aren’t errors that most developers encounter within their daily lives (unless you work with numbers often) and its likely even less often that you will actually encounter an actual Overflow exception. This is because by default within Visual Studio Arithmetic overflows a

Overflow and underflow aren’t errors that most developers encounter within their daily lives (unless you work with numbers often) and its likely even less often that you will actually encounter an actual Overflow exception. This is because by default 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 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.
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 :

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

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 was somewhat helpful for you.

License

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

Share

About the Author

Rion Williams
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.
Follow on   Twitter

Comments and Discussions

 
QuestionSmall type PinmemberMember 1091320529-Jun-14 19:43 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.1411028.1 | Last Updated 6 May 2013
Article Copyright 2013 by Rion Williams
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid