class Program
{ static string result1 = null; static void Main(string[] args) { string result= printresult(); Console.WriteLine("result Say :\nMy Value Is " + result); Console.WriteLine("result1 Say :\nMy Value Is" + result1); Console.ReadKey(); } static string printresult() { string s = "haha"; int a = 0; try { a = Convert.ToInt32(s);//异常出现 result1 = "i am trying..."; return "我try完了,回去"; } catch (Exception e) { result1 = "i am catch..."; return "我catch完了,回去"; } finally { result1="我finally完了,回去";//这里的语句不管try-catch结果如何,都会执行。但允许写return语句 } result1="完了,i have to go";//提示无法访问的代码,因为try和catch都有返回值,这句话没机会执行了 return result1; }//返回值是:"我catch完了,回去"; }
结论:finally语句无论try-catch结果如何都会执行,执行完才会执行try-catch中的return语句(如果有的话)