我不明白使用 try 和 catch 处理错误的意义。(C#) 在我的在线课程中,使用了以下示例:
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();
}
}
这是一个非常简单的例子,但我为什么要这样做呢?
我是新手……我正在学习编写 C# 代码……但我尝试以这种方式学习它,在最好的情况下根本不会发生错误。
为什么我会尝试捕获这样的异常而不是
run the programm -> "Ah there is a exeption" -> and then fix it?