public sealed class IndexOutOfRangeException : SystemException
Object
Exception
SystemException
IndexOutOfRangeException
mscorlib
BCL
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.
[Note: The following CIL instructions throw IndexOutOfRangeException :
]
- ldelem.<type>
- ldelema
- stelem.<type>
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()
System Namespace
IndexOutOfRangeException Constructors
IndexOutOfRangeException() Constructor
IndexOutOfRangeException(System.String) Constructor
IndexOutOfRangeException(System.String, System.Exception) Constructor
public IndexOutOfRangeException();
Constructs and initializes a new instance of the IndexOutOfRangeException class.
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
.
System.IndexOutOfRangeException Class, System Namespace
public IndexOutOfRangeException(string message);
Constructs and initializes a new instance of the IndexOutOfRangeException class.
- 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.
This constructor initializes the System.IndexOutOfRangeException.Message property of the new instance using message. If message isnull
, 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 tonull
.
System.IndexOutOfRangeException Class, System Namespace
public IndexOutOfRangeException(string message, Exception innerException);
Constructs and initializes a new instance of the IndexOutOfRangeException class.
- 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 .
This constructor initializes the System.IndexOutOfRangeException.Message property of the new instance using message, and the System.IndexOutOfRangeException.InnerException property using innerException. If message isnull
, 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.]
System.IndexOutOfRangeException Class, System Namespace