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.