# IndexOutOfRangeException

***

**1. Array Access**

```csharp
int[] arr = new int[5];
try
{
    Console.WriteLine(arr[5]);
}
catch (IndexOutOfRangeException e)
{
    Console.WriteLine("Array index out of range: {0}", e.Message);
}
```

**2. List Access**

```csharp
List<int> list = new List<int>();
try
{
    Console.WriteLine(list[5]);
}
catch (IndexOutOfRangeException e)
{
    Console.WriteLine("List index out of range: {0}", e.Message);
}
```

**3. String Access**

```csharp
string str = "Hello";
try
{
    Console.WriteLine(str[5]);
}
catch (IndexOutOfRangeException e)
{
    Console.WriteLine("String index out of range: {0}", e.Message);
}
```

**4. Dictionary Access**

```csharp
Dictionary<string, object> dict = new Dictionary<string, object>();
try
{
    Console.WriteLine(dict["nonexistent"]);
}
catch (KeyNotFoundException e)
{
    Console.WriteLine("Key not found: {0}", e.Message);
}
catch (IndexOutOfRangeException e)
{
    Console.WriteLine("Index out of range: {0}", e.Message);
}
```

**5. Queue Access**

```csharp
Queue<int> queue = new Queue<int>();
try
{
    Console.WriteLine(queue.Dequeue());
}
catch (IndexOutOfRangeException e)
{
    Console.WriteLine("Queue is empty: {0}", e.Message);
}
```

**6. Stack Access**

```csharp
Stack<int> stack = new Stack<int>();
try
{
    Console.WriteLine(stack.Pop());
}
catch (IndexOutOfRangeException e)
{
    Console.WriteLine("Stack is empty: {0}", e.Message);
}
```

**7. Collection Access**

```csharp
Collection<int> collection = new Collection<int>();
try
{
    Console.WriteLine(collection[5]);
}
catch (IndexOutOfRangeException e)
{
    Console.WriteLine("Collection index out of range: {0}", e.Message);
}
```

**8. ArrayList Access**

```csharp
ArrayList list = new ArrayList();
try
{
    Console.WriteLine(list[5]);
}
catch (IndexOutOfRangeException e)
{
    Console.WriteLine("ArrayList index out of range: {0}", e.Message);
}
```

**9. Hashtable Access**

```csharp
Hashtable hashtable = new Hashtable();
try
{
    Console.WriteLine(hashtable["nonexistent"]);
}
catch (KeyNotFoundException e)
{
    Console.WriteLine("Key not found: {0}", e.Message);
}
catch (IndexOutOfRangeException e)
{
    Console.WriteLine("Index out of range: {0}", e.Message);
}
```

**10. SortedList Access**

```csharp
SortedList<string, string> sortedList = new SortedList<string, string>();
try
{
    Console.WriteLine(sortedList["nonexistent"]);
}
catch (KeyNotFoundException e)
{
    Console.WriteLine("Key not found: {0}", e.Message);
}
catch (IndexOutOfRangeException e)
{
    Console.WriteLine("Index out of range: {0}", e.Message);
}
```

**11. Property Access**

```csharp
public class Person
{
    public string Name { get; set; }
}

Person person = new Person();
try
{
    Console.WriteLine(person.Age); // Age property does not exist
}
catch (IndexOutOfRangeException e)
{
    Console.WriteLine("Property index out of range: {0}", e.Message);
}
```

**12. Field Access**

```csharp
public class Person
{
    public int Id;
}

Person person = new Person();
try
{
    Console.WriteLine(person.Name); // Name field does not exist
}
catch (IndexOutOfRangeException e)
{
    Console.WriteLine("Field index out of range: {0}", e.Message);
}
```

**13. Pointer Access**

```csharp
unsafe
{
    int* ptr = null;
    try
    {
        Console.WriteLine(*ptr);
    }
    catch (IndexOutOfRangeException e)
    {
        Console.WriteLine("Pointer index out of range: {0}", e.Message);
    }
}
```

**14. XML Document Access**

```csharp
XmlDocument doc = new XmlDocument();
try
{
    Console.WriteLine(doc.DocumentElement.ChildNodes[5]);
}
catch (IndexOutOfRangeException e)
{
    Console.WriteLine("XML document index out of range: {0}", e.Message);
}
```

**15. JSON Document Access**

```csharp
JObject json = JObject.Parse("{ \"name\": \"John\" }");
try
{
    Console.WriteLine(json["age"]);
}
catch (IndexOutOfRangeException e)
{
    Console.WriteLine("JSON document index out of range: {0}", e.Message);
}
```

**16. Web API Response Access**

```csharp
HttpResponseMessage response = await client.GetAsync("api/values");
if (response.IsSuccessStatusCode)
{
    try
    {
        Console.WriteLine(response.Content.ReadAsAsync<List<int>>().Result[5]);
    }
    catch (IndexOutOfRangeException e)
    {
        Console.WriteLine("Web API response index out of range: {0}", e.Message);
    }
}
```

**17. Database Query Result Access**

```csharp
using System.Data.SqlClient;

using (SqlConnection connection = new SqlConnection("Server=.;Database=Northwind;Trusted_Connection=Yes"))
{
    using (SqlCommand command = new SqlCommand("SELECT * FROM Products", connection))
    {
        connection.Open();
        using (SqlDataReader reader = command.ExecuteReader())
        {
            try
            {
                Console.WriteLine(reader["ProductName", 5]);
            }
            catch (IndexOutOfRangeException e)
            {
                Console.WriteLine("Database query result index out of range: {0}", e.Message);
            }
        }
    }
}
```

**18. CSV File Access**

```csharp
using System.IO;

using (var reader = new StreamReader("data.csv"))
{
    string[] fields = reader.ReadLine().Split(',');
    try
    {
        Console.WriteLine(fields[5]);
    }
    catch (IndexOutOfRangeException e)
    {
        Console.WriteLine("CSV file index out of range: {0}", e.Message);
    }
}
```

**19. XML File Access**

```csharp
using System.IO;
using System.Xml;

using (var reader = XmlReader.Create("data.xml"))
{
    reader.Read();
    try
    {
        Console.WriteLine(reader["name"]);
    }
    catch (IndexOutOfRangeException e)
    {
        Console.WriteLine("XML file index out of range: {0}", e.Message);
    }
}
```

**20. INI File Access**

```csharp
using System.Configuration;

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
try
{
    Console.WriteLine(config.AppSettings.Settings["nonexistent"]);
}
catch (IndexOutOfRangeException e)
{
    Console.WriteLine("INI file index out of range: {0}", e.Message);
}
```

**21. Resource File Access**

```csharp
using System.Globalization;
using System.Resources;

var resources = new ResourceManager("resources", Assembly.GetExecutingAssembly());
try
{
    Console.WriteLine(resources.GetString("nonexistent", CultureInfo.CurrentCulture));
}
catch (IndexOutOfRangeException e)
{
    Console.WriteLine("Resource file index out of range: {0}", e.Message);
}
```

**22. Configuration File Access**

```csharp
using System.Configuration;

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
try
{
    Console.WriteLine(config.AppSettings.Settings["nonexistent"].Value);
}
catch (IndexOutOfRangeException e)
{
    Console.WriteLine("Configuration file index out of range: {0}", e.Message);
}
```

**23. Markdown File Access**

```csharp
using System.IO;
using System.Text.RegularExpressions;

using (var reader = new StreamReader("data.md"))
{
    string line = reader.ReadLine();
    Regex regex = new Regex(@"^#.*$");
    try
    {
        Console.WriteLine(regex.Matches(line)[5].Value);
    }
    catch (IndexOutOfRangeException e)
    {
        Console.WriteLine("Markdown file index out of range: {0}", e.Message);
    }
}
```

**24. YAML File Access**

```csharp
using System.IO;
using System.Text.RegularExpressions;

using (

```
