I don't understand the sense of error handling with try and catch. (C#) In my online course the following example was used:
class Program
{
static void Main(string[] args)
{
try
{
int[] numbers = new int[3];
numbers[1] = 0;
numbers[2] = 1;
numbers[3] = 2;
numbers[4] = 3;
}
catch(IndexOutOfRangeException ex)
{
Console.WriteLine(ex.Message);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadKey();
}
}
This is a very simple example, but why would I do that?
Im a newbie... im learning to code C# ... But I try to learn it this way that in the best case no mistakes occur at all.
Why would I try to catch an exception like that instead of
run the programm -> "Ah there is a exeption" -> and then fix it?