In this article about checked statement. The checked statements force fully C# to raise exception whenever underflow or stack overflow exception occurs due to integral type arithmetic or conversion issues.

Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CheckedStatementExample
{
    class Program
    {
        static void Main(string[] args)
        {
            int n;
            // assign maximum value
            n = int.MaxValue;
            try
            {
                checked
                {
                    // forces stack overflow exception
                    n = n + 1;
                    Console.WriteLine(n);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            Console.ReadLine();
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *