小编Tom*_*mmy的帖子

finally 块没有意义吗?

我正在自学 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)

.net c# try-catch-finally

2
推荐指数
1
解决办法
742
查看次数

标签 统计

.net ×1

c# ×1

try-catch-finally ×1