Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / tests / exception18.cs
1 using System;
2 using System.Linq;
3 using System.Runtime.CompilerServices;
4
5 class C
6 {
7         static Exception e;
8
9         static void Throw ()
10         {
11                 try {
12                         int.Parse (null);
13                 } catch (Exception ex) {
14                         e = ex;
15                 }
16         }
17
18         static int FrameCount (Exception ex)
19         {
20                         string fullTrace = ex.StackTrace;
21                         string[] frames = fullTrace.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
22
23                         // Ignore metadata
24                         frames = frames.Where (l => !l.StartsWith ("[")).ToArray ();
25
26                         return frames.Length;
27         }
28
29         public static void Main ()
30         {
31                 Throw ();
32
33                 try {
34                         throw e;
35                 } catch (Exception ex) {
36                         int frames = FrameCount (ex);
37                         if (frames != 1)
38                                 throw new Exception (String.Format("Exception carried {0} frames along with it when it should have reported one.", frames));
39                 }
40
41                 try {
42                         try {
43                                 int.Parse (null);
44                         } catch (Exception) {
45                                 throw;
46                         }
47                 } catch (Exception ex) {
48                         int frames = FrameCount (ex);
49                         if (frames != 4)
50                                 throw new Exception (String.Format("Exception carried {0} frames along with it when it should have reported four.", frames));
51                 }
52
53                 try {
54                         new C ().M1a ();
55                 } catch (Exception ex) {
56                         int frames = FrameCount (ex);
57                         if (frames != 4)
58                                 throw new Exception (String.Format("Exception carried {0} frames along with it when it should have reported four.", frames));
59                 }
60
61                 try {
62                         new C ().M1b ();
63                 } catch (Exception ex) {
64                         int frames = FrameCount (ex);
65                         if (frames != 3)
66                                 throw new Exception (String.Format("Exception carried {0} frames along with it when it should have reported three.", frames));
67                 }
68         }
69
70         [MethodImpl(MethodImplOptions.NoInlining)]
71         private void M1a ()
72         {
73                 M2a ();
74         }
75
76         [MethodImpl(MethodImplOptions.NoInlining)]
77         private void M1b ()
78         {
79                 M2b ();
80         }
81
82         [MethodImpl(MethodImplOptions.NoInlining)]
83         private void M2a ()
84         {
85                 try {
86                         M3 ();
87                 } catch {
88                         throw;
89                 }
90         }
91
92         [MethodImpl(MethodImplOptions.NoInlining)]
93         private void M2b ()
94         {
95                 try {
96                         M3 ();
97                 } catch (Exception ex) {
98                         throw ex;
99                 }
100         }
101
102         [MethodImpl(MethodImplOptions.NoInlining)]
103         private void M3 ()
104         {
105                 throw new NotImplementedException ();
106         }
107 }