Enabling Underflow And Overflow Checking Within Visual Studio

Overflow and underflow aren’t errors that most developers encounter within their daily lives and it’s 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.

  1. var crazy = int.MaxValue; // 2,147,483,647   
  2. 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:

Build setting

  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.
  5. Within the Advanced Build Settings dialog, check the Check for arithmetic overflow / underflow check-box.

You can use the previous 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!

Which can be seen below if you are a more visual person:

OverflowException

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:

  1. checked   
  2. {  
  3.    var crazy = int.MaxValue;   
  4.    crazy++; // This will throw an overflow exception since it is wrapped within a checked block  
  5. }  
And as an operator (to just check a single expression):
  1. var crazy = int.MaxValue;   
  2. 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.