以下面的代码为例:
try
{
Response.Redirect(someurl);
}
finally
{
// Will this code run?
}
Run Code Online (Sandbox Code Playgroud)
finally块中的代码会运行吗?
简单到足以测试:
try
{
Response.Redirect(someurl);
}
finally
{
File.WriteAllText("C:\\Temp\\test.txt", "The finally block ran.");
}
Run Code Online (Sandbox Code Playgroud)
它会运行.Response.Redirect实际上会抛出一个ThreadAbortException,这就是为什么之后的代码不能运行(当然,除了finally块中的任何东西).