Today, in the interview,this question be asked, but I cant't  answer because I am cpluspluser before, I turn to learn C# not long.

  So, what the different between throw and throw e? I found a amasing answer here when I  search some information with google.

  I run the program in vs2008, the code is : 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace DiffBetweenThrowAndThrowE
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             try
13             {
14                 ThrowException1(); // line 14
15             }
16             catch (Exception x)
17             {
18                 Console.WriteLine("Exception 1:");
19                 Console.WriteLine(x.StackTrace);
20             }
21 
22             try
23             {
24                 ThrowException2(); // line 24
25             }
26             catch (Exception x)
27             {
28                 Console.WriteLine("Exception 2:");
29                 Console.WriteLine(x.StackTrace);
30             }
31         }
32 
33         private static void ThrowException1()
34         {
35             try
36             {
37                 DivByZero(); // line 37
38             }
39             catch
40             {
41                 throw; // line 41
42             }
43         }
44 
45         private static void ThrowException2()
46         {
47             try
48             {
49                 DivByZero(); // line 49
50             }
51             catch (Exception ex)
52             {
53                 throw ex; // line 53
54             }
55         }
56 
57         private static void DivByZero()
58         {
59             int x = 0;
60             int y = 1 / x; // line 60
61         }
62     }
63 }
View Code

相关文章: