Merge pull request #439 from mono-soc-2012/garyb/iconfix
[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         [ComVisible(true)]
44         [ComDefaultInterface (typeof (_Exception))]
45         [ClassInterface (ClassInterfaceType.None)]
46         [StructLayout (LayoutKind.Sequential)]
47         public class Exception : ISerializable, _Exception
48         {
49 #pragma warning disable 169, 649
50                 #region Sync with object-internals.h
51                 /* Stores the IPs and the generic sharing infos
52                    (vtable/MRGCTX) of the frames. */
53                 IntPtr [] trace_ips;
54                 Exception inner_exception;
55                 internal string message;
56                 string help_link;
57                 string class_name;
58                 string stack_trace;
59                 // formerly known as remote_stack_trace (see #425512):
60                 string _remoteStackTraceString;
61                 int remote_stack_index;
62                 internal int hresult = -2146233088;
63                 string source;
64                 IDictionary _data;
65                 StackTrace[] captured_traces;
66                 IntPtr[] native_trace_ips;
67                 #endregion
68 #pragma warning restore 169, 649
69
70                 /* Don't add fields here, the runtime depends on the layout of subclasses */
71
72                 public Exception ()
73                 {
74                 }
75
76                 public Exception (string message)
77                 {
78                         this.message = message;
79                 }
80
81                 protected Exception (SerializationInfo info, StreamingContext context)
82                 {
83                         if (info == null)
84                                 throw new ArgumentNullException ("info");
85
86                         class_name          = info.GetString ("ClassName");
87                         message             = info.GetString ("Message");
88                         help_link           = info.GetString ("HelpURL");
89                         stack_trace         = info.GetString ("StackTraceString");
90                         _remoteStackTraceString  = info.GetString ("RemoteStackTraceString");
91                         remote_stack_index  = info.GetInt32  ("RemoteStackIndex");
92                         hresult             = info.GetInt32  ("HResult");
93                         source              = info.GetString ("Source");
94                         inner_exception     = (Exception) info.GetValue ("InnerException", typeof (Exception));
95
96                         try {
97                                 _data = (IDictionary) info.GetValue ("Data", typeof (IDictionary));
98                         } catch (SerializationException) {
99                                 // member did not exist in .NET 1.x
100                         }
101                 }
102
103                 public Exception (string message, Exception innerException)
104                 {
105                         inner_exception = innerException;
106                         this.message = message;
107                 }
108
109                 public Exception InnerException {
110                         get { return inner_exception; }
111                 }
112
113                 public virtual string HelpLink {
114                         get { return help_link; }
115                         set { help_link = value; }
116                 }
117
118 #if NET_4_5
119                 public int HResult {
120                         get { return hresult; }
121                         protected set { hresult = value; }
122                 }
123 #else
124                 protected int HResult {
125                         get { return hresult; }
126                         set { hresult = value; }
127                 }
128 #endif
129                 internal void SetMessage (string s)
130                 {
131                         message = s;
132                 }
133
134                 internal void SetStackTrace (string s)
135                 {
136                         stack_trace = s;
137                 }
138
139                 string ClassName {
140                         get {
141                                 if (class_name == null)
142                                         class_name = GetType ().ToString ();
143                                 return class_name;
144                         }
145                 }
146
147                 public virtual string Message {
148                         get {
149                                 if (message == null)
150                                         message = string.Format (Locale.GetText ("Exception of type '{0}' was thrown."),
151                                                 ClassName);
152
153                                 return message;
154                         }
155                 }
156                 
157 #if NET_4_0
158                 [MonoTODO]
159                 protected event EventHandler<SafeSerializationEventArgs> SerializeObjectState {
160                         add {
161                         }
162                         remove {
163                         }
164                 }
165 #endif
166
167                 public virtual string Source {
168                         get {
169                                 if (source == null) {
170                                         StackTrace st = new StackTrace (this, true);
171                                         if (st.FrameCount > 0) {
172                                                 StackFrame sf = st.GetFrame (0);
173                                                 if (st != null) {
174                                                         MethodBase method = sf.GetMethod ();
175                                                         if (method != null) {
176                                                                 source = method.DeclaringType.Assembly.UnprotectedGetName ().Name;
177                                                         }
178                                                 }
179                                         }
180                                 }
181
182                                 // source can be null
183                                 return source;
184                         }
185
186                         set {
187                                 source = value;
188                         }
189                 }
190
191                 void AddFrames (StringBuilder sb, string newline, string unknown, StackTrace st) {
192                         for (int i = 0; i < st.FrameCount; i++) {
193                                 StackFrame frame = st.GetFrame (i);
194                                 if (i == 0)
195                                         sb.AppendFormat ("  {0} ", Locale.GetText ("at"));
196                                 else
197                                         sb.Append (newline);
198
199                                 if (frame.GetMethod () == null) {
200                                         string internal_name = frame.GetInternalMethodName ();
201                                         if (internal_name != null)
202                                                 sb.Append (internal_name);
203                                         else
204                                                 sb.AppendFormat ("<0x{0:x5}> {1}", frame.GetNativeOffset (), unknown);
205                                 } else {
206                                         GetFullNameForStackTrace (sb, frame.GetMethod ());
207
208                                         if (frame.GetILOffset () == -1)
209                                                 sb.AppendFormat (" <0x{0:x5}> ", frame.GetNativeOffset ());
210                                         else
211                                                 sb.AppendFormat (" [0x{0:x5}] ", frame.GetILOffset ());
212
213                                         sb.AppendFormat ("in {0}:{1} ", frame.GetSecureFileName (),
214                                                                          frame.GetFileLineNumber ());
215                                 }
216                         }
217                 }
218
219                 public virtual string StackTrace {
220                         get {
221                                 if (stack_trace != null)
222                                         return stack_trace;
223
224                                 if (trace_ips == null)
225                                         /* Not thrown yet */
226                                         return null;
227
228                                 StringBuilder sb = new StringBuilder ();
229
230                                 string newline = String.Format ("{0}  {1} ", Environment.NewLine, Locale.GetText ("at"));
231                                 string unknown = Locale.GetText ("<unknown method>");
232
233                                 // Add traces captured using ExceptionDispatchInfo
234                                 if (captured_traces != null) {
235                                         foreach (var t in captured_traces) {
236                                                 AddFrames (sb, newline, unknown, t);
237                                                 sb.Append (Environment.NewLine);
238                                                 sb.Append ("--- End of stack trace from previous location where exception was thrown ---");
239                                                 sb.Append (Environment.NewLine);
240                                         }
241                                 }
242
243                                 StackTrace st = new StackTrace (this, 0, true, true);
244                                 AddFrames (sb, newline, unknown, st);
245
246                                 stack_trace = sb.ToString ();
247
248                                 return stack_trace;
249                         }
250                 }
251
252                 public MethodBase TargetSite {
253                         get {
254                                 StackTrace st = new StackTrace (this, true);
255                                 if (st.FrameCount > 0)
256                                         return st.GetFrame (0).GetMethod ();
257                                 
258                                 return null;
259                         }
260                 }
261
262                 public virtual IDictionary Data {
263                         get {
264                                 if (_data == null) {
265                                         // default to empty dictionary
266                                         _data = (IDictionary) new Hashtable ();
267                                 }
268                                 return _data;
269                         }
270                 }
271
272                 public virtual Exception GetBaseException ()
273                 {
274                         Exception inner = inner_exception;
275                                 
276                         while (inner != null)
277                         {
278                                 if (inner.InnerException != null)
279                                         inner = inner.InnerException;
280                                 else
281                                         return inner;
282                         }
283
284                         return this;
285                 }
286
287                 [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)]
288                 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
289                 {
290                         if (info == null)
291                                 throw new ArgumentNullException ("info");
292
293                         info.AddValue ("ClassName", ClassName);
294                         info.AddValue ("Message", message);
295                         info.AddValue ("InnerException", inner_exception);
296                         info.AddValue ("HelpURL", help_link);
297                         info.AddValue ("StackTraceString", StackTrace);
298                         info.AddValue ("RemoteStackTraceString", _remoteStackTraceString);
299                         info.AddValue ("RemoteStackIndex", remote_stack_index);
300                         info.AddValue ("HResult", hresult);
301 #if !MOONLIGHT
302                         info.AddValue ("Source", Source);
303 #else
304                         info.AddValue ("Source", null);
305 #endif
306                         info.AddValue ("ExceptionMethod", null);
307                         info.AddValue ("Data", _data, typeof (IDictionary));
308                 }
309
310                 public override string ToString ()
311                 {
312                         System.Text.StringBuilder result = new System.Text.StringBuilder (ClassName);
313                         result.Append (": ").Append (Message);
314
315                         if (null != _remoteStackTraceString)
316                                 result.Append (_remoteStackTraceString);
317                                 
318                         if (inner_exception != null) 
319                         {
320                                 result.Append (" ---> ").Append (inner_exception.ToString ());
321                                 result.Append (Environment.NewLine);
322                                 result.Append (Locale.GetText ("  --- End of inner exception stack trace ---"));
323                         }
324
325                         if (StackTrace != null)
326                                 result.Append (Environment.NewLine).Append (StackTrace);
327                         return result.ToString();
328                 }
329
330                 internal Exception FixRemotingException ()
331                 {
332                         string message = (0 == remote_stack_index) ?
333                                 Locale.GetText ("{0}{0}Server stack trace: {0}{1}{0}{0}Exception rethrown at [{2}]: {0}") :
334                                 Locale.GetText ("{1}{0}{0}Exception rethrown at [{2}]: {0}");
335                         string tmp = String.Format (message, Environment.NewLine, StackTrace, remote_stack_index);
336
337                         _remoteStackTraceString = tmp;
338                         remote_stack_index++;
339
340                         stack_trace = null;
341
342                         return this;
343                 }
344
345                 internal void GetFullNameForStackTrace (StringBuilder sb, MethodBase mi)
346                 {
347                         ParameterInfo[] p = mi.GetParameters ();
348                         sb.Append (mi.DeclaringType.ToString ());
349                         sb.Append (".");
350                         sb.Append (mi.Name);
351
352                         if (mi.IsGenericMethod) {
353                                 Type[] gen_params = mi.GetGenericArguments ();
354                                 sb.Append ("[");
355                                 for (int j = 0; j < gen_params.Length; j++) {
356                                         if (j > 0)
357                                                 sb.Append (",");
358                                         sb.Append (gen_params [j].Name);
359                                 }
360                                 sb.Append ("]");
361                         }
362
363                         sb.Append (" (");
364                         for (int i = 0; i < p.Length; ++i) {
365                                 if (i > 0)
366                                         sb.Append (", ");
367                                 Type pt = p[i].ParameterType;
368                                 if (pt.IsClass && !String.IsNullOrEmpty (pt.Namespace)) {
369                                         sb.Append (pt.Namespace);
370                                         sb.Append (".");
371                                 }
372                                 sb.Append (pt.Name);
373                                 if (p [i].Name != null) {
374                                         sb.Append (" ");
375                                         sb.Append (p [i].Name);
376                                 }
377                         }
378                         sb.Append (")");
379                 }
380
381                 // For ExceptionDispatchInfo
382                 internal void CaptureTrace () {
383                         if (captured_traces != null) {
384                                 StackTrace[] new_traces = new StackTrace [captured_traces.Length + 1];
385                                 Array.Copy (captured_traces, new_traces, captured_traces.Length);
386                                 captured_traces = new_traces;
387                         } else {
388                                 captured_traces = new StackTrace [1];
389                         }
390                         captured_traces [captured_traces.Length - 1] = new StackTrace (this, 0, true, true);
391
392                         trace_ips = null;
393                 }
394
395                 //
396                 // The documentation states that this is available in 1.x,
397                 // but it was not available (MemberRefing this would fail)
398                 // and it states the signature is `override sealed', but the
399                 // correct value is `newslot' 
400                 //
401                 public new Type GetType ()
402                 {
403                         return base.GetType ();
404                 }
405         }
406 }