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