C#同级catch块和finally块中全都抛出异常,上一级优先捕获finally块中的异常。
测试代码:
1 using System; 2 3 namespace test 4 { 5 class Program 6 { 7 static void Main(string[] args) 8 { 9 try10 {11 try12 {13 throw new Exception();14 }15 catch (Exception)16 {17 throw new Exception("catch块儿");18 }19 finally20 {21 throw new Exception("finally块儿");22 }23 }24 catch (Exception e)25 {26 Console.WriteLine(e.Message);27 }28 Console.Read();29 }30 }31 }
运行结果: