You may have come across questions,

  • What is checked and unchecked primitive operations?
  • How to handle overflow in C-Sharp?
  • Does CLR handles overflow or developers?

So let us start with understanding a program. Consider the code listed below:

 


    static void Main(string[] args)
        {
           
                Byte b = 100;
                b = (Byte) (b + 200);
                Console.WriteLine(b);
         
            Console.ReadKey(true);
        }


On running you will get 44 as the output. There is one serious observation about the above output is that Bye is primitive type which is unsigned 8 bit value. Even though above add operation results an overflow, CLR is not complaining about that.

By default, CLR does not throw overflow exception.

However there may be a requirement in which, you may have to throw the overflow exception. CLR gives us the option to configure whether overflow exception should be thrown or not.

We can configure this at three level

  1. Statements level.
  2. Operations level
  3. Application level

Application level

You can configure to throw overflow exception at the project level. To configure, right click on the project, select properties and then build, and then advanced. In the advanced build setting check the checkbox- check for arithmetic overflow/underflow

clip_image002

After configuring check for arithmetic overflow/underflow at the project level, if you go ahead and run the application, CLR will throw an exception as shown next,

clip_image004

We are getting the overflow exception since we have configured at the project level that throw exception on arithmetic overflow/underflow.

Statements level

You may not want to throw the exception for all the operations in the project. So you can also configure whether to throw overflow/underflow exception or not at the statement or expression level.

C# provides us checked and unchecked operator and statement to configure overflow/underflow exception at the operation and statement level respectively.

Using the check statement, you can throw overflow exception in above code as listed below:

 


static void Main(string[] args)
        {
            checked
            {

                Byte b = 100;
                b = (Byte)(b + 200);
                Console.WriteLine(b);
            }
               
         
            Console.ReadKey(true);
        }


By using the check statement, we can as CLR to throw overflow or underflow exception caused by the arithmetic operations.

Operations level

C# also allows us to apply cheeked/unchecked operator on a single expression. Above code can be written as listed next:

 

static void Main(string[] args)
        {
                Byte b = 100;
                b = checked ((Byte)(b + 200));
                Console.WriteLine(b);
                Console.ReadKey(true);
        }


Understanding Internal

CLR has two instructions for each arithmetic operations.

Addition: add and add.ovf

Subtraction: sub and sub.ovf

Multiplication: mul and mul.ovf

Conversion: conv and conv.ovf

As instruction name suggests add instruction does not throw overflow exception whereas add.ovf throws overflow exception. When we perform add operation by default CLR executes add IL instruction. However when we perform checked add operation, CLR executes add.ovf exception which will throw overflow exception.

Summary

There were three questions in the beginning, let us see them one by one

What is checked and unchecked primitive operations?

Checked and unchecked operations check for the overflow in the arithmetic operations. If checked then operation may throw the overflow/underflow exception. If unchecked then it will not throw the overflow exception.

How to handle overflow in C-Sharp?

Using the checked and unchecked overflow can be handled in the C#.

Does CLR handles overflow or developers?

By default CLR does not throw overflow exception. However using checked and unchecked developer can configure whether to throw or not.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s