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

总结,以上例子很是简单,但是在实际开发中,咱们老是会不经意忽略一些细节,最终致使上了生产后异常格外的难以追踪。

相关文章
相关标签/搜索