Permalink
Fetching contributors…
Cannot retrieve contributors at this time
46 lines (38 sloc) 908 Bytes
// <Snippet1>
using System;
public class NumberGenerator
{
int[] numbers = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
public int GetNumber(int index)
{
if (index < 0 || index >= numbers.Length) {
throw new IndexOutOfRangeException();
}
return numbers[index];
}
}
// </Snippet1>
namespace TestException
{
// <Snippet2>
using System;
public class Example
{
public static void Main()
{
var gen = new NumberGenerator();
int index = 10;
try {
int value = gen.GetNumber(index);
Console.WriteLine($"Retrieved {value}");
}
catch (IndexOutOfRangeException e)
{
Console.WriteLine($"{e.GetType().Name}: {index} is outside the bounds of the array");
}
}
}
// The example displays the following output:
// IndexOutOfRangeException: 10 is outside the bounds of the array
// </Snippet2>
}