try块或catch块结束或return后,如果有finally块,则一定会在return之前执行,且finally中不允许有return。若finally块中对返回的变量做了修改,最终返回结果仍以try块或catch块中的值为准。
static void Main(string[] args)
{
int t=method();
Console.WriteLine("method_return:a={0}",t);
Console.Read();
}
public static int method()
{
int a = 1;
try
{
return a;
}
catch(Exception ex)
{
return a;
}
finally
{
a = 2;
Console.WriteLine("finally:a={0}",a);
}
}