An attempt to write to a nonexistent array element : Array Exception : Data Structure C# Examples


C# Examples » Data Structure » Array Exception »

 

An attempt to write to a nonexistent array element









    
using  System;

class  MainClass
{

    public  static  void  Main()
    {

        try
        {
            int[]  intArray  =  new  int[5];
            for  (int  i  =  0;  i  <=  intArray.Length;  i++)
            {
                intArray[i]  =  i;
                Console.WriteLine("intArray["  +  i  +  "]  =  "  +  intArray[i]);
            }
        }
        catch  (IndexOutOfRangeException  e)
        {
            Console.WriteLine("IndexOutOfRangeException  occurred");
            Console.WriteLine("Message  =  "  +  e.Message);
            Console.WriteLine("Stack  trace  =  "  +  e.StackTrace);
        }
    }
}
    
   
  
   



Output

intArray[0] = 0
intArray[1] = 1
intArray[2] = 2
intArray[3] = 3
intArray[4] = 4
IndexOutOfRangeException occurred
Message = Index was outside the bounds of the array.
Stack trace =    at MainClass.Main()


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Data Structure
» Array Exception