
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to capture index out of range exception in C#?
IndexOutOfRangeException occurs when you try to access an element with an index that is outsise the bounds of the array.
Let’s say the following is our array. It has 5 elements −
int [] n = new int[5] {66, 33, 56, 23, 81};
Now if you will try to access elements with index more than 5, then the IndexOutOfRange Exception is thrown −
for (j = 0; j < 10; j++ ) { Console.WriteLine("Element[{0}] = {1}", j, n[j]); }
In the above example, we are trying to access above index 5, therefore the following error occurs −
System.IndexOutOfRangeException: Index was outside the bounds of the array.
Here is the complete code −
Example
using System; namespace Demo { class MyArray { static void Main(string[] args) { try { int [] n = new int[5] {66, 33, 56, 23, 81}; int i,j; // error: IndexOutOfRangeException for (j = 0; j < 10; j++ ) { Console.WriteLine("Element[{0}] = {1}", j, n[j]); } Console.ReadKey(); } catch (System.IndexOutOfRangeException e) { Console.WriteLine(e); } } } }
Output
Element[0] = 66 Element[1] = 33 Element[2] = 56 Element[3] = 23 Element[4] = 81 System.IndexOutOfRangeException: Index was outside the bounds of the array. at Demo.MyArray.Main (System.String[] args) [0x00019] in <6ff1dbe1755b407391fe21dec35d62bd>:0
The code will generate an error −
System.IndexOutOfRangeException −Index was outside the bounds of the array.
- Related Articles
- How to capture out of array index out of bounds exception in Java?
- How to capture out of memory exception in C#?
- How to capture null reference exception in C#?
- How to capture divide by zero exception in C#?
- How to capture file not found exception in C#?
- How to handle Java Array Index Out of Bounds Exception?
- How to capture divide by zero exception in Java?
- How to capture file not found exception in Java?
- How to capture and print Python exception message?
- Why substring slicing index out of range works in Python?
- How to capture an exception raised by a Python Regular expression?
- How to solve “Process out of Memory Exception” in Node.js?
- Golang program to delete the ith index node, when index is the out of range in the linked list.
- Count of Palindromic substrings in an Index range in C++
- Out of memory exception in Java:\n

Advertisements