在日常項目開發中,異常拋出和捕獲是再平常不過的事情。通過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行。
總結,以上例子非常簡單,可是在實際開發中,我們總是會不經意忽略一些細節,最終導致上了生產后異常格外的難以追蹤。