New test.
[mono.git] / mcs / class / corlib / System / Exception.cs
1 //
2 // System.Exception.cs
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //   Patrik Torstensson
7 //
8 // (C) Ximian, Inc.  http://www.ximian.com
9 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.Collections;
32 using System.Diagnostics;
33 using System.Reflection;
34 using System.Text;
35 using System.Runtime.InteropServices;
36 using System.Runtime.CompilerServices;
37 using System.Runtime.Serialization;
38 using System.Security.Permissions;
39
40 namespace System
41 {
42         [Serializable]
43         [ClassInterface (ClassInterfaceType.AutoDual)]
44 #if NET_2_0
45         [ComVisible(true)]
46 #endif
47         public class Exception : ISerializable 
48 #if NET_2_0
49         , _Exception
50 #endif
51         {
52                 IntPtr [] trace_ips;
53                 Exception inner_exception;
54                 internal string message;
55                 string help_link;
56                 string class_name;
57                 string stack_trace;
58                 string remote_stack_trace;
59                 int remote_stack_index;
60                 internal int hresult = unchecked ((int)0x80004005);
61                 string source;
62
63                 public Exception ()
64                 {
65                 }
66
67                 public Exception (string msg)
68                 {
69                         message = msg;
70                 }
71
72                 protected Exception (SerializationInfo info, StreamingContext sc)
73                 {
74                         if (info == null)
75                                 throw new ArgumentNullException ("info");
76
77                         class_name          = info.GetString ("ClassName");
78                         message             = info.GetString ("Message");
79                         help_link           = info.GetString ("HelpURL");
80                         stack_trace         = info.GetString ("StackTraceString");
81                         remote_stack_trace  = info.GetString ("RemoteStackTraceString");
82                         remote_stack_index  = info.GetInt32  ("RemoteStackIndex");
83                         hresult             = info.GetInt32  ("HResult");
84                         source              = info.GetString ("Source");
85                         inner_exception     = (Exception) info.GetValue ("InnerException", typeof (Exception));
86                 }
87
88                 public Exception (string msg, Exception e)
89                 {
90                         inner_exception = e;
91                         message = msg;
92                 }
93
94                 public Exception InnerException {
95                         get { return inner_exception; }
96                 }
97
98                 public virtual string HelpLink {
99                         get { return help_link; }
100                         set { help_link = value; }
101                 }
102
103                 protected int HResult {
104                         get { return hresult; }
105                         set { hresult = value; }
106                 }
107
108                 internal void SetMessage (string s)
109                 {
110                         message = s;
111                 }
112
113                 internal void SetStackTrace (string s)
114                 {
115                         stack_trace = s;
116                 }
117
118                 public virtual string Message {
119                         get {
120                                 if (message == null)
121                                         message = string.Format (Locale.GetText ("Exception of type {0} was thrown."), GetType ().ToString());
122
123                                 return message;
124                         }
125                 }
126
127                 public virtual string Source {
128 #if ONLY_1_1
129                         [ReflectionPermission (SecurityAction.Assert, TypeInformation = true)]
130 #endif
131                         get {
132                                 if (source == null) {
133                                         StackTrace st = new StackTrace (this, true);
134                                         if (st.FrameCount > 0) {
135                                                 StackFrame sf = st.GetFrame (0);
136                                                 if (st != null) {
137                                                         MethodBase method = sf.GetMethod ();
138                                                         if (method != null) {
139                                                                 source = method.DeclaringType.Assembly.UnprotectedGetName ().Name;
140                                                         }
141                                                 }
142                                         }
143                                 }
144
145                                 // source can be null
146                                 return source;
147                         }
148
149                         set {
150                                 source = value;
151                         }
152                 }
153
154                 public virtual string StackTrace {
155                         get {
156                                 if (stack_trace == null) {
157                                         if (trace_ips == null)
158                                                 /* Not thrown yet */
159                                                 return null;
160
161                                         StackTrace st = new StackTrace (this, 0, true, true);
162
163                                         StringBuilder sb = new StringBuilder ();
164
165                                         string newline = String.Format ("{0}  {1} ", Environment.NewLine, Locale.GetText ("at"));
166                                         string unknown = Locale.GetText ("<unknown method>");
167
168                                         for (int i = 0; i < st.FrameCount; i++) {
169                                                 StackFrame frame = st.GetFrame (i);
170                                                 if (i == 0)
171                                                         sb.AppendFormat ("  {0} ", Locale.GetText ("at"));
172                                                 else
173                                                         sb.Append (newline);
174
175                                                 if (frame.GetMethod () == null) {
176                                                         string internal_name = frame.GetInternalMethodName ();
177                                                         if (internal_name != null)
178                                                                 sb.Append (internal_name);
179                                                         else
180                                                                 sb.AppendFormat ("<0x{0:x5}> {1}", frame.GetNativeOffset (), unknown);
181                                                 } else {
182                                                         sb.Append (GetFullNameForStackTrace (frame.GetMethod ()));
183
184                                                         if (frame.GetILOffset () == -1)
185                                                                 sb.AppendFormat (" <0x{0:x5}> ", frame.GetNativeOffset ());
186                                                         else
187                                                                 sb.AppendFormat (" [0x{0:x5}] ", frame.GetILOffset ());
188
189                                                         string fileName = frame.GetFileName ();
190                                                         if (fileName != null)
191                                                                 sb.AppendFormat ("in {0}:{1} ", fileName, frame.GetFileLineNumber ());
192                                                         }
193                                         }
194                                         stack_trace = sb.ToString ();
195                                 }
196
197                                 return stack_trace;
198                         }
199                 }
200
201                 public MethodBase TargetSite {
202 #if ONLY_1_1
203                         [ReflectionPermission (SecurityAction.Demand, TypeInformation = true)]
204 #endif
205                         get {
206                                 StackTrace st = new StackTrace (this, true);
207                                 if (st.FrameCount > 0)
208                                         return st.GetFrame (0).GetMethod ();
209                                 
210                                 return null;
211                         }
212                 }
213
214 #if NET_2_0
215                 private IDictionary _data;
216
217                 public virtual IDictionary Data {
218                         get {
219                                 if (_data == null) {
220                                         // default to empty dictionary
221                                         _data = (IDictionary) new Hashtable ();
222                                 }
223                                 return _data;
224                         }
225                 }
226 #endif
227
228                 public virtual Exception GetBaseException ()
229                 {
230                         Exception inner = inner_exception;
231                                 
232                         while (inner != null)
233                         {
234                                 if (inner.InnerException != null)
235                                         inner = inner.InnerException;
236                                 else
237                                         return inner;
238                         }
239
240                         return this;
241                 }
242
243 #if ONLY_1_1
244                 [ReflectionPermission (SecurityAction.Assert, TypeInformation = true)]
245 #endif
246                 [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)]
247                 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
248                 {
249                         if (info == null)
250                                 throw new ArgumentNullException ("info");
251
252                         if (class_name == null)
253                                 class_name = GetType ().FullName;
254
255                         info.AddValue ("ClassName", class_name);
256                         info.AddValue ("Message", message);
257                         info.AddValue ("InnerException", inner_exception);
258                         info.AddValue ("HelpURL", help_link);
259                         info.AddValue ("StackTraceString", StackTrace);
260                         info.AddValue ("RemoteStackTraceString", remote_stack_trace);
261                         info.AddValue ("RemoteStackIndex", remote_stack_index);
262                         info.AddValue ("HResult", hresult);
263                         info.AddValue ("Source", Source);
264                         info.AddValue ("ExceptionMethod", null);
265                 }
266
267 #if ONLY_1_1
268                 [ReflectionPermission (SecurityAction.Assert, TypeInformation = true)]
269 #endif
270                 public override string ToString ()
271                 {
272                         System.Text.StringBuilder result = new System.Text.StringBuilder (this.GetType ().FullName);
273                         result.Append (": ").Append (Message);
274
275                         if (null != remote_stack_trace)
276                                 result.Append (remote_stack_trace);
277                                 
278                         if (inner_exception != null) 
279                         {
280                                 result.Append (" ---> ").Append (inner_exception.ToString ());
281                                 result.Append (Locale.GetText ("--- End of inner exception stack trace ---"));
282                                 result.Append (Environment.NewLine);
283                         }
284
285                         if (StackTrace != null)
286                                 result.Append (Environment.NewLine).Append (StackTrace);
287                         return result.ToString();
288                 }
289
290                 internal Exception FixRemotingException ()
291                 {
292                         string message = (0 == remote_stack_index) ?
293                                 Locale.GetText ("{0}{0}Server stack trace: {0}{1}{0}{0}Exception rethrown at [{2}]: {0}") :
294                                 Locale.GetText ("{1}{0}{0}Exception rethrown at [{2}]: {0}");
295                         string tmp = String.Format (message, Environment.NewLine, StackTrace, remote_stack_index);
296
297                         remote_stack_trace = tmp;
298                         remote_stack_index++;
299
300                         stack_trace = null;
301
302                         return this;
303                 }
304
305                 internal string GetFullNameForStackTrace (MethodBase mi)
306                 {
307                         string parms = String.Empty;
308                         ParameterInfo[] p = mi.GetParameters ();
309                         for (int i = 0; i < p.Length; ++i) {
310                                 if (i > 0)
311                                         parms = parms + ", ";
312                                 string paramName = (p [i].Name == null) ? String.Empty : (" " + p [i].Name);
313                                 Type pt = p[i].ParameterType;
314                                 if (pt.IsClass && pt.Namespace != String.Empty)
315                                         parms = parms + pt.Namespace + "." + pt.Name + paramName;
316                                 else
317                                         parms = parms + pt.Name + paramName;
318                         }
319
320                         string generic = String.Empty;
321 #if NET_2_0 || BOOTSTRAP_NET_2_0
322                         if (mi.IsGenericMethod) {
323                                 Type[] gen_params = mi.GetGenericArguments ();
324                                 generic = "[";
325                                 for (int j = 0; j < gen_params.Length; j++) {
326                                         if (j > 0)
327                                                 generic += ",";
328                                         generic += gen_params [j].Name;
329                                 }
330                                 generic += "]";
331                         }
332 #endif
333                         return mi.DeclaringType.ToString () + "." + mi.Name + generic + " (" + parms + ")";
334                 }
335
336 #if NET_2_0
337                 //
338                 // The documentation states that this is available in 1.x,
339                 // but it was not available (MemberRefing this would fail)
340                 // and it states the signature is `override sealed', but the
341                 // correct value is `newslot' 
342                 //
343                 public new Type GetType ()
344                 {
345                         return base.GetType ();
346                 }
347 #endif
348         }
349 }