using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(A());
}
static int A()
{
try
{
Console.WriteLine("try");
int a = Convert.ToInt32("a");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return 1;
}
finally
{
Console.WriteLine("finally");
}
return 2;
}
}
}
结果
总结:
1.finally中的内容总会被执行,就算catch中已经return。
2.如果catch中有return,则程序会在执行finally后结束,方法返回的值是catch中的。
3.finally中不可以写return.
本文通过一个具体的C#示例程序详细解释了try-catch-finally语句块的工作原理,包括finally块的执行时机、catch中return的影响及finally中不可使用return的原因。
2161

被折叠的 条评论
为什么被折叠?



