System.IndexOutOfRangeException Class

public sealed class IndexOutOfRangeException : SystemException

Base Types

Object
  Exception
    SystemException
      IndexOutOfRangeException

Assembly

mscorlib

Library

BCL

Summary

Represents the error that occurs when an attempt is made to access an element of an array with an index that is outside the bounds of the array.

Description

[Note: The following CIL instructions throw IndexOutOfRangeException :

]

Example

The following example demonstrates an error that causes a IndexOutOfRangeException exception.

using System;
public class IndexRangeTest {
 public static void Main() {
   int[] array = {0,0,0};
   try {
     for (int i = 0; i<4; i++) {
       Console.WriteLine("array[{0}] = {1}",i,array[i]);
     }
   }
   catch (IndexOutOfRangeException e) {
     Console.WriteLine("Error: {0}",e);
   }
 }
}
   
The output is

array[0] = 0
array[1] = 0
array[2] = 0
Error: System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at IndexRangeTest.Main()
 

See Also

System Namespace

Members

IndexOutOfRangeException Constructors

IndexOutOfRangeException() Constructor
IndexOutOfRangeException(System.String) Constructor
IndexOutOfRangeException(System.String, System.Exception) Constructor


IndexOutOfRangeException() Constructor

public IndexOutOfRangeException();

Summary

Constructs and initializes a new instance of the IndexOutOfRangeException class.

Description

This constructor initializes the System.IndexOutOfRangeException.Message property of the new instance to a system-supplied message that describes the error, such as "An array index is out of range." This message takes into account the current system culture.

The System.IndexOutOfRangeException.InnerException property is initialized to null .

See Also

System.IndexOutOfRangeException Class, System Namespace

IndexOutOfRangeException(System.String) Constructor

public IndexOutOfRangeException(string message);

Summary

Constructs and initializes a new instance of the IndexOutOfRangeException class.

Parameters

message
A String that describes the error. The content of message is intended to be understood by humans. The caller of this constructor is required to ensure that this string has been localized for the current system culture.

Description

This constructor initializes the System.IndexOutOfRangeException.Message property of the new instance using message. If message is null , the System.IndexOutOfRangeException.Message property is initialized to the system-supplied message provided by the constructor that takes no arguments. The System.IndexOutOfRangeException.InnerException property is initialized to null .

See Also

System.IndexOutOfRangeException Class, System Namespace

IndexOutOfRangeException(System.String, System.Exception) Constructor

public IndexOutOfRangeException(string message, Exception innerException);

Summary

Constructs and initializes a new instance of the IndexOutOfRangeException class.

Parameters

message
A String that describes the error. The content of message is intended to be understood by humans. The caller of this constructor is required to ensure that this string has been localized for the current system culture.
innerException
An instance of Exception that is the cause of the current exception. If innerException is non-null, then the current exception was raised in a catch block handling innerException .

Description

This constructor initializes the System.IndexOutOfRangeException.Message property of the new instance using message, and the System.IndexOutOfRangeException.InnerException property using innerException. If message is null , the System.IndexOutOfRangeException.Message property is initialized to the system-supplied message provided by the constructor that takes no arguments.

[Note: For information on inner exceptions, see System.Exception.InnerException.]

See Also

System.IndexOutOfRangeException Class, System Namespace