Merge pull request #901 from Blewzman/FixAggregateExceptionGetBaseException
[mono.git] / mono / tests / finalizer-exception.cs
1 using System;
2 using System.Threading;
3
4 public class FinalizerException {
5
6         ~FinalizerException () {
7                 throw new Exception ();
8         }
9
10         static IntPtr aptr;
11
12         /*
13          * We allocate the exception object deep down the stack so
14          * that it doesn't get pinned.
15          */
16         public static unsafe void MakeException (int depth) {
17                 // Avoid tail calls
18                 int* values = stackalloc int [20];
19                 aptr = new IntPtr (values);
20                 if (depth <= 0) {
21                         new FinalizerException ();
22                         return;
23                 }
24                 MakeException (depth - 1);
25         }
26
27         public static int Main () { 
28                 AppDomain.CurrentDomain.UnhandledException += (sender, args) => {
29                         Console.WriteLine ("caught");
30                         Environment.Exit (0);
31                 };
32
33                 MakeException (1024);
34
35                 GC.Collect ();
36                 GC.WaitForPendingFinalizers ();
37
38                 Thread.Sleep (Timeout.Infinite); // infinite wait so we don't race against the unhandled exception callback
39
40                 return 2;
41         }
42 }