c# 异常精准定位

在日常项目开发中,异常抛出和捕获是再平常不过的事情。通过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行。

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

 

posted @ 2019-11-26 16:02  skydau  阅读(4113)  评论(4编辑  收藏  举报