Friday, February 18, 2011

Exceptions in C#

unexpected situation can be handle using Exceptions in C#,exception used try,catch and finally keywords,try block having statements or code to be executed. If try block not executed successfully then it throws catch exception to show error and catch is known as exception handler and finally is the exception executed either error come or not.
lets understand the concept with the program Index out of bound.

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {  int[] y = new int[3];     //Array declaration of type int
            try
            { 
                Console.WriteLine("Enter array");
                for ( int i= 0; i <= 5; i++)
                {
                   y[i] = Convert.ToInt32(Console.ReadLine());
               }
            }
            catch
            {
                Console.WriteLine("Index out of bound");
            }
            finally
            {
                Console.WriteLine("final exception");
                Console.ReadLine();
                 }
            }
    }
}

No comments:

Popular Posts