2 using System.Threading;
4 public class FinalizerException {
5 ~FinalizerException () {
6 throw new Exception ();
10 * We allocate the exception object deep down the stack so
11 * that it doesn't get pinned.
13 public static unsafe void MakeException (int depth) {
15 int* values = stackalloc int [20];
17 new FinalizerException ();
20 MakeException (depth - 1);
23 public static int Main () {
24 AppDomain.CurrentDomain.UnhandledException += (sender, args) => {
25 Console.WriteLine ("caught");
32 GC.WaitForPendingFinalizers ();
34 Thread.Sleep (Timeout.Infinite); // infinite wait so we don't race against the unhandled exception callback