标签:lin nal ESS catch test line OLE console 执行
面试常会被问及try-catch-finally,现在做一下总结:
第一、不管有没有出现异常,finally块中代码都会执行。
第二、finally 代码块中不能有return。
第三、如果try 或catch中有return语句,则在finally中对返回变量的修改不会影响 返回值。
举例如下:
虽然在finally中对变量x进行了修改,但并会影响到返回值(x=2)。
static int test()
{
int x = 1;
try
{
x++;
return x;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return x;
}
finally
{
++x;
Console.WriteLine("执行finally");
}
}
C#常见面试题(一)——try-catch-finally-return
标签:lin nal ESS catch test line OLE console 执行
原文地址:https://www.cnblogs.com/liangxiarong/p/10116678.html