我正在自学 C#,我要学习 try、catch 和 finally。我正在使用的这本书讨论了 finally 块如何运行,而不管 try 块是否成功。但是,即使不在 finally 中,写在 catch 块之外的代码也不会运行吗?如果是这样,最后的意义是什么?这是本书提供的示例程序:
class myAppClass
{
public static void Main()
{
int[] myArray = new int[5];
try
{
for (int ctr = 0; ctr <10; ctr++)
{
myArray[ctr] = ctr;
}
}
catch
{
Console.WriteLine("Exception caught");
}
finally
{
Console.WriteLine("Done with exception handling");
}
Console.WriteLine("End of Program");
Console.ReadLine();
}
}
Run Code Online (Sandbox Code Playgroud)