我假设 finally block 总是在程序运行时执行。但是,在此控制台应用程序中,finally block 似乎并未执行。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
try
{
throw new Exception();
}
finally
{
Console.WriteLine("finally");
}
}
}
}
输出
注意:抛出异常时,windows 询问我是否要结束应用程序,我说"is"。
最佳答案
它实际执行了。只是你没有注意到。就在您看到 Windows 正在检查问题的解决方案
时,单击 Cancel 并查看它。
关于c# - 为什么 finally 没有被执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14262744/