c# 異常精準定位

在平常項目開發中,異常拋出和捕獲是再日常不過的事情。經過try-catch咱們能夠方便的捕獲異常,同時經過查看異常堆棧咱們能發現拋出異常代碼的位置。ide

例以下面這段代碼:spa

 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 }

 

這段代碼很是簡單,運行後他拋出了以下異常:code

System.DivideByZeroException: Attempted to divide by zero.
   at ExamplesProject.Program.Main(String[] args) in D:\Projects\ExamplesProject\ExamplesProject\Program.cs:line 16

沒有問題,堆棧信息明確指出了拋出異常的位置,也正是除零異常發生的位置。對象

假如由於種種緣由,須要把main方法的代碼邏輯抽取到另外的方法裏,像下面這樣:blog

 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的方法本身捕獲並從新拋出了異常。開發

再來看一下異常信息:string

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行。it

發生了什麼??爲何不是第30行。io

再來看一個例子。class

假如業務變複雜了,代碼又嵌套了一層。調用關係變複雜了,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行。

總結,以上例子很是簡單,但是在實際開發中,咱們老是會不經意忽略一些細節,最終致使上了生產後異常格外的難以追蹤。

相關文章
相關標籤/搜索