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 void MakeException (int depth) {
15 new FinalizerException ();
18 MakeException (depth - 1);
21 public static int Main () {
22 AppDomain.CurrentDomain.UnhandledException += (sender, args) => {
23 Console.WriteLine ("caught");
30 GC.WaitForPendingFinalizers ();
32 Thread.Sleep (Timeout.Infinite); // infinite wait so we don't race against the unhandled exception callback