2 using System.Threading;
4 public class FinalizerException {
6 ~FinalizerException () {
7 throw new Exception ();
13 * We allocate the exception object deep down the stack so
14 * that it doesn't get pinned.
16 public static unsafe void MakeException (int depth) {
18 int* values = stackalloc int [20];
19 aptr = new IntPtr (values);
21 new FinalizerException ();
24 MakeException (depth - 1);
27 public static int Main () {
28 AppDomain.CurrentDomain.UnhandledException += (sender, args) => {
29 Console.WriteLine ("caught");
36 GC.WaitForPendingFinalizers ();
38 Thread.Sleep (Timeout.Infinite); // infinite wait so we don't race against the unhandled exception callback