2 using System.Collections.Generic;
4 class StackTraceDumper {
6 public static void Main ()
9 throw new Exception ("Stacktrace with 1 frame");
10 } catch (Exception e) {
11 Console.WriteLine (e);
12 Console.WriteLine ("Stacktrace:");
13 Console.WriteLine (new System.Diagnostics.StackTrace(e));
16 Catch (() => {throw new Exception ("Stacktrace with 2 frames");});
18 Catch (() => ThrowException ("Stacktrace with 3 frames", 1));
20 Catch (() => ThrowException ("Stacktrace with 4 frames", 2));
23 var message = "Stack frame with method overload using ref parameter";
24 ThrowException (ref message);
29 ThrowException ("Stack frame with method overload using out parameter", out i);
32 Catch (() => ThrowExceptionGeneric<double> ("Stack frame with 1 generic parameter"));
34 Catch (() => ThrowExceptionGeneric<double,string> ("Stack frame with 2 generic parameters"));
36 Catch (() => ThrowExceptionGeneric (12));
38 Catch (() => InnerClass.ThrowException ("Stack trace with inner class"));
40 Catch (() => InnerGenericClass<string>.ThrowException ("Stack trace with inner generic class"));
42 Catch (() => InnerGenericClass<string>.ThrowException ("Stack trace with inner generic class and method generic parameter", "string"));
44 Catch (() => InnerGenericClass<string>.ThrowException<string> ("Stack trace with inner generic class and generic overload", "string"));
46 Catch (() => InnerGenericClass<string>.InnerInnerGenericClass<int>.ThrowException ("Stack trace with 2 inner generic class and generic overload"));
48 Catch (() => InnerGenericClass<int>.InnerInnerGenericClass<string>.ThrowException ("Stack trace with 2 inner generic class and generic overload"));
51 var d = new Dictionary<string, string> ();
52 d.ContainsKey (null); // ArgumentNullException
56 public static void Catch (Action action)
60 } catch (Exception e) {
62 Console.WriteLine (e);
63 Console.WriteLine ("Stacktrace:");
64 Console.WriteLine (new System.Diagnostics.StackTrace (e));
68 public static void ThrowException (string message)
70 throw new Exception (message);
73 public static void ThrowException (ref string message)
75 throw new Exception (message);
78 public static void ThrowException (string message, int i)
81 ThrowException (message, --i);
83 throw new Exception (message);
86 public static void ThrowException (string message, out int o)
88 throw new Exception (message);
91 public static void ThrowExceptionGeneric<T> (string message)
93 throw new Exception (message);
96 public static void ThrowExceptionGeneric<T> (T a1)
98 throw new Exception ("Stack frame with generic method overload");
101 public static void ThrowExceptionGeneric<T> (List<string> a1)
103 throw new Exception ("Stack frame with generic method overload");
106 public static void ThrowExceptionGeneric<T> (List<T> a1)
108 throw new Exception ("Stack frame with generic method overload");
111 public static void ThrowExceptionGeneric<T1,T2> (string message)
113 throw new Exception (message);
117 public static void ThrowException (string message)
119 throw new Exception (message);
123 class InnerGenericClass<T> {
124 public static void ThrowException (string message)
126 throw new Exception (message);
129 public static void ThrowException (string message, T arg)
131 Console.WriteLine ("Generic to string:" + arg.ToString());
132 throw new Exception (message);
135 public static void ThrowException<T1> (string message, T1 arg)
137 throw new Exception (message);
140 public class InnerInnerGenericClass<T2> {
141 public static void ThrowException (T message)
143 throw new Exception (message as string);
146 public static void ThrowException (T2 message)
148 throw new Exception (message as string);