Overflow Exception Handling Using Checked and Unchecked

In this Section you will learn:
  • What is Checked and Unchecked
  • How to get correct result while using small size variable in arithmetic operation like byte, sbyte or short?
  • How to handle overflow exception with checked and unchecked statement?

As a C# programmer, you always should be aware of a correct output of the arithmetic operation. Your code might be correct, there is no runtime exception still your output may be incorrect and it may cause a serious problem in your project. To understand this theory, see this programming example first.

Programming Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Checked_Unchecked
{
    class Program
    {
        static void Main(string[] args)
        {
            sbyte num1 = 20, num2 = 30, result;
            result = (sbyte)(num1 * num2);
            Console.WriteLine("{0} x {1} = {2}", num1, num2, result);
            Console.ReadKey();               
        }
    }
}

Output

20 x 30 = 88
__
 

Explanation

In this example, we have used sbyte variable for simple arithmetic calculation. The multiply of num1 and num2 should be 600 but you will get output 88. It will not raise any exception and error still your program is incorrect. It is done because you have used very small variable without using checked andunchecked statement. The size of sbyte is -128 to 127 only; so the result is overflowed.

How to handle overflow exception using checked and unchecked statement?

It is very easy to fix this problem using checked and unchecked statement.Checked strictly monitor your code and if any overflow exception generated it sends control to catch for handling exception. Unchecked statement ignores overflow exception and shows output.

To understand it, see the programming example of checked and unchecked statements.

Programming Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Checked_Unchecked
{
    class Program
    {
        static void Main(string[] args)
        {
            sbyte num1 = 20, num2 = 30, result;
            try
            {
                unchecked
                {
                    result = (sbyte)(num1 * num2);
                    Console.WriteLine("{0} x {1} = {2}", num1, num2, result);
                }
                checked
                {
                    result = (sbyte)(num1 * num2);
                    Console.WriteLine("{0} x {1} = {2}", num1, num2, result);
                }
            }
            catch (OverflowException oex)
            {
                Console.WriteLine(oex.Message);
            }
            Console.ReadKey();
        }
    }
}

Output


20 x 30 = 88

Arithmetic operation resulted in an overflow.
__

Explanation

In this example, we have the same program as above but in the checked andunchecked block with exception handling. You will see two outputs; the first one is unchecked output that is 88 and second one is overflow exception message.

Note: Most of the beginners always make this mistake while creating project thus the project shows the incorrect result. It is not harmful to use checked and unchecked statements everywhere so while doing a project which is mostly based on arithmetic calculation you must be strict on overflow exception.

Summary

In this chapter you have learned how to handle overflow exception using checked and unchecked statement in c#. It is very important to be aware of incorrect output while using small size variables. In the next chapter you will understand exception handling more clearly with some programming examples.

 

Share your thought