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