在日常專案開發中,例外拋出和捕獲是再平常不過的事情,通過try-catch我們可以方便的捕獲例外,同時通過查看例外堆疊我們能發現拋出例外代碼的位置,
例如下面這段代碼:
1 using System; 2 using System.IO; 3 using System.Runtime.CompilerServices; 4 using System.Runtime.InteropServices; 5 6 namespace ExamplesProject 7 { 8 class Program 9 { 10 static int m = 0; 11 static void Main(string[] args) 12 { 13 try 14 { 15 int a = 10, b = 0; 16 Console.WriteLine(a / b); 17 } 18 catch (Exception ex) 19 { 20 Console.WriteLine(ex.ToString()); 21 } 22 23 Console.ReadLine(); 24 } 25 } 26 }
這段代碼非常簡單,運行后他拋出了如下例外:
System.DivideByZeroException: Attempted to divide by zero. at ExamplesProject.Program.Main(String[] args) in D:\Projects\ExamplesProject\ExamplesProject\Program.cs:line 16
沒有問題,堆疊資訊明確指出了拋出例外的位置,也正是除零例外發生的位置,
假如因為種種原因,需要把main方法的代碼邏輯抽取到另外的方法里,像下面這樣:
1 using System; 2 using System.IO; 3 using System.Runtime.CompilerServices; 4 using System.Runtime.InteropServices; 5 6 namespace ExamplesProject 7 { 8 class Program 9 { 10 static int m = 0; 11 static void Main(string[] args) 12 { 13 try 14 { 15 Divide(); 16 } 17 catch (Exception ex) 18 { 19 Console.WriteLine(ex.ToString()); 20 } 21 22 Console.ReadLine(); 23 } 24 25 public static void Divide() 26 { 27 try 28 { 29 int a = 10, b = 0; 30 Console.WriteLine(a / b); 31 } 32 catch (Exception ex) 33 { 34 throw ex; 35 } 36 } 37 } 38 }
這段代碼乍一看,好像也沒有什么問題,divide的方法自己捕獲并重新拋出了例外,
再來看一下例外資訊:
System.DivideByZeroException: Attempted to divide by zero. at ExamplesProject.Program.Divide() in D:\Projects\ExamplesProject\ExamplesProject\Program.cs:line 34 at ExamplesProject.Program.Main(String[] args) in D:\Projects\ExamplesProject\ExamplesProject\Program.cs:line 15
例外堆疊顯示,拋出例外的方法是Divide(),例外代碼位置是34行,可這里并不是例外真正的發生位置,真正的例外位置是第30行,
發生了什么??為什么不是第30行,
再來看一個例子,
假如業務變復雜了,代碼又嵌套了一層,呼叫關系變復雜了,main()->divide()->divide1(),
1 using System; 2 using System.IO; 3 using System.Runtime.CompilerServices; 4 using System.Runtime.InteropServices; 5 6 namespace ExamplesProject 7 { 8 class Program 9 { 10 static int m = 0; 11 static void Main(string[] args) 12 { 13 try 14 { 15 Divide(); 16 } 17 catch (Exception ex) 18 { 19 Console.WriteLine(ex.ToString()); 20 } 21 22 Console.ReadLine(); 23 } 24 25 public static void Divide() 26 { 27 try 28 { 29 Divide1(); 30 } 31 catch (Exception ex) 32 { 33 throw ex; 34 } 35 } 36 37 public static void Divide1() 38 { 39 try 40 { 41 int a = 10, b = 0; 42 Console.WriteLine(a / b); 43 } 44 catch (Exception ex) 45 { 46 throw ex; 47 } 48 } 49 } 50 }
運行代碼,拋出如下例外:
System.DivideByZeroException: Attempted to divide by zero. at ExamplesProject.Program.Divide() in D:\Projects\ExamplesProject\ExamplesProject\Program.cs:line 33 at ExamplesProject.Program.Main(String[] args) in D:\Projects\ExamplesProject\ExamplesProject\Program.cs:line 15
為什么例外位置是33行,而不是42行呢,代碼嵌套的越深,錯誤隱藏的越深,如果是在大型系統中,將很難發現錯誤的真正位置,
作者就犯過這樣的錯誤啊,問題到底在哪里呢?
問題的根源是,以上代碼拋出的例外并沒有體現層次關系,或者說例外的嵌套關系,理論上講,divide1()方法中catch捕獲到的例外為最內層例外,catch捕獲后處理完,如果要向上層拋出,應該重新實體化一個新的例外物件,并將當前例外作為其innerException, 再向上拋出,以此類推,divide()方法捕獲處理后如果要拋出例外也應該這樣做,這樣最外層的main方法catch到的例外才是完整的例外,自然包含完整的堆疊資訊,錯誤定位就是精準的,
改造后的例子:
1 using System; 2 using System.IO; 3 using System.Runtime.CompilerServices; 4 using System.Runtime.InteropServices; 5 6 namespace ExamplesProject 7 { 8 class Program 9 { 10 static int m = 0; 11 static void Main(string[] args) 12 { 13 try 14 { 15 Divide(); 16 } 17 catch (Exception ex) 18 { 19 Console.WriteLine(ex.ToString()); 20 } 21 22 Console.ReadLine(); 23 } 24 25 public static void Divide() 26 { 27 try 28 { 29 Divide1(); 30 } 31 catch (Exception ex) 32 { 33 throw new Exception(ex.Message, ex); 34 } 35 } 36 37 public static void Divide1() 38 { 39 try 40 { 41 int a = 10, b = 0; 42 Console.WriteLine(a / b); 43 } 44 catch (Exception ex) 45 { 46 throw new Exception(ex.Message, ex); 47 } 48 } 49 } 50 }
運行后,拋出如下例外:
System.Exception: Attempted to divide by zero. ---> System.Exception: Attempted to divide by zero. ---> System.DivideByZeroException: Attempted to divide by zero. at ExamplesProject.Program.Divide1() in D:\Projects\ExamplesProject\ExamplesProject\Program.cs:line 42 --- End of inner exception stack trace --- at ExamplesProject.Program.Divide1() in D:\Projects\ExamplesProject\ExamplesProject\Program.cs:line 46 at ExamplesProject.Program.Divide() in D:\Projects\ExamplesProject\ExamplesProject\Program.cs:line 29 --- End of inner exception stack trace --- at ExamplesProject.Program.Divide() in D:\Projects\ExamplesProject\ExamplesProject\Program.cs:line 33 at ExamplesProject.Program.Main(String[] args) in D:\Projects\ExamplesProject\ExamplesProject\Program.cs:line 15
這一次,例外堆疊精準的定位了例外代碼行第42行,
總結,以上例子非常簡單,可是在實際開發中,我們總是會不經意忽略一些細節,最終導致上了生產后例外格外的難以追蹤,
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/100041.html
標籤:C#
