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 for (int i
= 0; i
< 10; i
++)
22 new FinalizerException ();
25 MakeException (depth
- 1);
28 public static int Main () {
29 AppDomain
.CurrentDomain
.UnhandledException
+= (sender
, args
) => {
30 Console
.WriteLine ("caught");
34 var t
= new Thread (delegate () { MakeException (1024); }
);
39 GC
.WaitForPendingFinalizers ();
41 Thread
.Sleep (Timeout
.Infinite
); // infinite wait so we don't race against the unhandled exception callback