C# Program to Demonstrate IndexOutOfRange Exception

This is a C# Program to demonstrate IndexOutOfRange exception.

Problem Description

This C# Program Demonstrates IndexOutOfRange Exception.

Problem Solution

Here if the array has the index value out of the range that is specified then this exception is thrown.

Program/Source Code

Here is source code of the C# Program to Demonstrate IndexOutOfRange Exception. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Demonstrate IndexOutOfRange Exception
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace differnce
{
    class arrayoutofindex
    {
        public void calculatedifference()
        {
            int difference=0;
            int [] number= new int[5] {1,2,3,4,5};
            try
            {
                for (int init =1; init <=5; init++)
                {
                    difference= difference -  number[init];
                }
                Console.WriteLine("The difference of the array is:" + difference);
            }
            catch (IndexOutOfRangeException e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
    class classmain
    {
        static void Main(string [] args)
    {
        arrayoutofindex obj = new arrayoutofindex();
        obj.calculatedifference();
        Console.ReadLine();
    }
}
}
Program Explanation

This C# program is used to demonstrate IndexOutOfRange exception. Create an object ‘ob’ variable to arrayoutofindex class, then using object variable perform the calculateddifference() procedure.

advertisement
advertisement

In calculatedifference() procedure assign an value to the number[] array variable. In try statement for loop is used to compute the difference between the value of ‘variable’ by the value of ‘number[]’ array variable.

If the array has the index value out of the range that is specified then the exception is thrown. Using try and catch, an error message is displayed when the error occurs.

Runtime Test Cases
 
Index was outside the bounds of the array

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.