ArrayIndexOutOfBoundsException **Java** में होता है। C# में इसका समकक्ष IndexOutOfRangeException है।
उदाहरण: IndexOutOfRangeException
using System;
namespace Demo
{
class Program
{
static void Main(string[] args)
{
int[] arr = new int[3]; // इंडेक्स 0, 1, 2
arr[0] = 5;
arr[1] = 7;
arr[2] = 8;
arr[4] = 24; // ❌ IndexOutOfRangeException
}
}
}
आउटपुट
Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.
तुलना
| भाषा | एक्सेप्शन | नेमस्पेस |
|---|---|---|
| Java | ArrayIndexOutOfBoundsException | java.lang |
| C# | IndexOutOfRangeException | System |
दोनों में समान
- ऐरे/कलेक्शन की सीमा से बाहर इंडेक्स एक्सेस पर फेंका जाता है।
- रनटाइम एक्सेप्शन (अनचेक्ड)।
- बाउंड्स चेकिंग रनटाइम पर होती है।
C# में हैंडलिंग
try
{
int val = arr[index];
}
catch (IndexOutOfRangeException ex)
{
Console.WriteLine("अमान्य इंडेक्स: " + ex.Message);
}