Suppose I have the following situation:
9 class Program
10 {
11 public static void WrapperMethod(Action func)
12 {
13 try
14 {
15 //throw new Exception("Case 1");
16 func.Invoke();
17 }
18 catch (Exception ex)
19 {
20 Console.WriteLine(ex.StackTrace);
21 }
22 }
23
24 static void Main(string[] args)
25 {
26 WrapperMethod(() => { throw new Exception("Case 2"); });
27 }
28 }
I run it and have the following output:
at TestExceptions.Program.<Main>b__0() in c:\users\administrator\documents\visual studio 2010\Projects\TestExceptions\TestExceptions\Program.cs:line 26
at TestExceptions.Program.WrapperMethod(Action func) in c:\users\administrator\documents\visual studio 2010\Projects\TestExceptions\TestExceptions\Program.cs:line 16
If I uncomment throw new Exception(“Case 1”);
the output is:
at TestExceptions.Program.WrapperMethod(Action func) in c:\users\administrator\documents\visual studio 2010\Projects\TestExceptions\TestExceptions\Program.cs:line 15
So my question is why in the first case I can see the full path including the Main function while I cannot see the same stuff in the second case.
How can I display the more complete information if the production code is similar to the second case scenario.
In the first case you are making another method call to the anonymous method that is defined inside the
Mainmethod. As the exception is thrown inside the anonymous method, it’s included in the call stack.If you throw the exception in the
WrapperMethodmethod, then the anonymous method is never involved, so it won’t show up in the call stack.