
1 /// <summary>
2 /// 入口方法
3 /// </summary>
4 public static void Main()
5 {
6 ExceptionClass ec = new ExceptionClass();
7
8 try
9 {
10 ec.ExceptionThrow1();
11 }
12 catch (Exception ex)
13 {
14 Console.WriteLine(ex.ToString());
15 }
16
17 Console.WriteLine("---------------------------------------------------------------------");
18
19 try
20 {
21 ec.ExceptionThrow2();
22 }
23 catch (Exception ex)
24 {
25 Console.WriteLine(ex.ToString());
26 }
27
28 Console.WriteLine("---------------------------------------------------------------------");
29
30 try
31 {
32 ec.ExceptionThrow3();
33 }
34 catch (Exception ex)
35 {
36 Console.WriteLine(ex.ToString());
37 }
38
39 Console.WriteLine("---------------------------------------------------------------------");
40
41 try
42 {
43 ec.ExceptionThrow4();
44 }
45 catch (Exception ex)
46 {
47 Console.WriteLine(ex.ToString());
48 }
49
50 Console.WriteLine("---------------------------------------------------------------------");
51
52 Console.ReadKey();
53 }
54 }
55
56 /// <summary>
57 /// 该Class用来测试异常抛出时相关上下文栈的调用情况
58 /// </summary>
59 public class ExceptionClass
60 {
61 /// <summary>
62 /// 抛出异常方法
63 /// </summary>
64 public void ExceptionThrow1()
65 {
66 try
67 {
68 // 调用原始异常抛出方法来抛出异常
69 this.ExceptionMethod();
70 }
71 catch (Exception ex)
72 {
73 throw ex;
74 }
75 }
76
77 /// <summary>
78 /// 抛出异常方法1
79 /// </summary>
80 public void ExceptionThrow2()
81 {
82 try
83 {
84 this.ExceptionMethod();
85 }
86 catch (Exception ex)
87 {
88 throw;
89 }
90 }
91
92 /// <summary>
93 /// 抛出异常方法2
94 /// </summary>
95 public void ExceptionThrow3()
96 {
97 try
98 {
99 this.ExceptionMethod();
100 }
101 catch
102 {
103 throw;
104 }
105 }
106
107 /// <summary>
108 /// 抛出异常方法3
109 /// </summary>
110 public void ExceptionThrow4()
111 {
112 try
113 {
114 this.ExceptionMethod();
115 }
116 catch (Exception ex)
117 {
118 throw new Exception("经过进一步包装的异常", ex);
119 }
120 }
121
122 /// <summary>
123 /// 原始异常抛出方法
124 /// </summary>
125 private void ExceptionMethod()
126 {
127 throw new DivideByZeroException();
128 }
129 }
