Throw
using System;
// We can throw exceptions to indicate errors that occur in our programs by using throw keyword.
public class ThrowDemo
{
static void Main(String[] args)
{
int[] a = new int[] { 1, 2 };
for (int i = 0; i <= 2; i++)
{
&bsp; if (i == 2)
{
throw new IndexOutOfRangeException("Out of range");
}
Console.WriteLine(a[i]);
}
Console.ReadLine();
}
}
Output:
1
2
IndexOutOfRangeException was unhandled
Out of range