5 // Miguel de Icaza (miguel@ximian.com)
8 // (C) Ximian, Inc. http://www.ximian.com
9 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
31 using System.Collections;
32 using System.Collections.Generic;
33 using System.Diagnostics;
34 using System.Reflection;
36 using System.Runtime.InteropServices;
37 using System.Runtime.CompilerServices;
38 using System.Runtime.Serialization;
39 using System.Security.Permissions;
45 [ComDefaultInterface (typeof (_Exception))]
46 [ClassInterface (ClassInterfaceType.None)]
47 [StructLayout (LayoutKind.Sequential)]
49 public class Exception : ISerializable
51 public class Exception : ISerializable, _Exception
54 #pragma warning disable 169, 649
55 #region Sync with object-internals.h
56 /* Stores the IPs and the generic sharing infos
57 (vtable/MRGCTX) of the frames. */
59 Exception inner_exception;
60 internal string message;
64 // formerly known as remote_stack_trace (see #425512):
65 string _remoteStackTraceString;
66 int remote_stack_index;
67 internal int hresult = -2146233088;
70 StackTrace[] captured_traces;
71 IntPtr[] native_trace_ips;
73 #pragma warning restore 169, 649
75 /* Don't add fields here, the runtime depends on the layout of subclasses */
81 public Exception (string message)
83 this.message = message;
86 protected Exception (SerializationInfo info, StreamingContext context)
89 throw new ArgumentNullException ("info");
91 class_name = info.GetString ("ClassName");
92 message = info.GetString ("Message");
93 help_link = info.GetString ("HelpURL");
94 stack_trace = info.GetString ("StackTraceString");
95 _remoteStackTraceString = info.GetString ("RemoteStackTraceString");
96 remote_stack_index = info.GetInt32 ("RemoteStackIndex");
97 hresult = info.GetInt32 ("HResult");
98 source = info.GetString ("Source");
99 inner_exception = (Exception) info.GetValue ("InnerException", typeof (Exception));
102 _data = (IDictionary) info.GetValue ("Data", typeof (IDictionary));
103 } catch (SerializationException) {
104 // member did not exist in .NET 1.x
108 public Exception (string message, Exception innerException)
110 inner_exception = innerException;
111 this.message = message;
114 public Exception InnerException {
115 get { return inner_exception; }
118 public virtual string HelpLink {
119 get { return help_link; }
120 set { help_link = value; }
125 get { return hresult; }
126 protected set { hresult = value; }
129 protected int HResult {
130 get { return hresult; }
131 set { hresult = value; }
134 internal void SetMessage (string s)
139 internal void SetStackTrace (string s)
146 if (class_name == null)
147 class_name = GetType ().ToString ();
152 public virtual string Message {
155 message = string.Format (Locale.GetText ("Exception of type '{0}' was thrown."),
164 protected event EventHandler<SafeSerializationEventArgs> SerializeObjectState {
172 public virtual string Source {
174 if (source == null) {
175 StackTrace st = new StackTrace (this, true);
176 if (st.FrameCount > 0) {
177 StackFrame sf = st.GetFrame (0);
179 MethodBase method = sf.GetMethod ();
180 if (method != null) {
181 source = method.DeclaringType.Assembly.UnprotectedGetName ().Name;
187 // source can be null
196 bool AddFrames (StringBuilder sb, string newline, string unknown, StackTrace st)
199 for (i = 0; i < st.FrameCount; i++) {
200 StackFrame frame = st.GetFrame (i);
202 sb.AppendFormat (" {0} ", Locale.GetText ("at"));
206 if (frame.GetMethod () == null) {
207 string internal_name = frame.GetInternalMethodName ();
208 if (internal_name != null)
209 sb.Append (internal_name);
211 sb.AppendFormat ("<0x{0:x5}> {1}", frame.GetNativeOffset (), unknown);
213 GetFullNameForStackTrace (sb, frame.GetMethod ());
215 if (frame.GetILOffset () == -1)
216 sb.AppendFormat (" <0x{0:x5}> ", frame.GetNativeOffset ());
218 sb.AppendFormat (" [0x{0:x5}] ", frame.GetILOffset ());
220 sb.AppendFormat ("in {0}:{1} ", frame.GetSecureFileName (),
221 frame.GetFileLineNumber ());
228 public virtual string StackTrace {
230 if (stack_trace != null)
233 if (trace_ips == null)
237 StringBuilder sb = new StringBuilder ();
239 string newline = String.Format ("{0} {1} ", Environment.NewLine, Locale.GetText ("at"));
240 string unknown = Locale.GetText ("<unknown method>");
242 // Add traces captured using ExceptionDispatchInfo
243 if (captured_traces != null) {
244 foreach (var t in captured_traces) {
245 if (!AddFrames (sb, newline, unknown, t))
248 sb.Append (Environment.NewLine);
249 sb.Append ("--- End of stack trace from previous location where exception was thrown ---");
250 sb.Append (Environment.NewLine);
254 StackTrace st = new StackTrace (this, 0, true, true);
255 AddFrames (sb, newline, unknown, st);
257 stack_trace = sb.ToString ();
263 public MethodBase TargetSite {
265 StackTrace st = new StackTrace (this, true);
266 if (st.FrameCount > 0)
267 return st.GetFrame (0).GetMethod ();
273 public virtual IDictionary Data {
276 // default to empty dictionary
277 _data = new Dictionary<object, object> ();
283 public virtual Exception GetBaseException ()
285 Exception inner = inner_exception;
287 while (inner != null)
289 if (inner.InnerException != null)
290 inner = inner.InnerException;
298 [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)]
299 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
302 throw new ArgumentNullException ("info");
304 info.AddValue ("ClassName", ClassName);
305 info.AddValue ("Message", message);
306 info.AddValue ("InnerException", inner_exception);
307 info.AddValue ("HelpURL", help_link);
308 info.AddValue ("StackTraceString", StackTrace);
309 info.AddValue ("RemoteStackTraceString", _remoteStackTraceString);
310 info.AddValue ("RemoteStackIndex", remote_stack_index);
311 info.AddValue ("HResult", hresult);
312 info.AddValue ("Source", Source);
313 info.AddValue ("ExceptionMethod", null);
314 info.AddValue ("Data", _data, typeof (IDictionary));
317 public override string ToString ()
319 System.Text.StringBuilder result = new System.Text.StringBuilder (ClassName);
320 result.Append (": ").Append (Message);
322 if (null != _remoteStackTraceString)
323 result.Append (_remoteStackTraceString);
325 if (inner_exception != null)
327 result.Append (" ---> ").Append (inner_exception.ToString ());
328 result.Append (Environment.NewLine);
329 result.Append (Locale.GetText (" --- End of inner exception stack trace ---"));
332 if (StackTrace != null)
333 result.Append (Environment.NewLine).Append (StackTrace);
334 return result.ToString();
337 internal Exception FixRemotingException ()
339 string message = (0 == remote_stack_index) ?
340 Locale.GetText ("{0}{0}Server stack trace: {0}{1}{0}{0}Exception rethrown at [{2}]: {0}") :
341 Locale.GetText ("{1}{0}{0}Exception rethrown at [{2}]: {0}");
342 string tmp = String.Format (message, Environment.NewLine, StackTrace, remote_stack_index);
344 _remoteStackTraceString = tmp;
345 remote_stack_index++;
352 internal void GetFullNameForStackTrace (StringBuilder sb, MethodBase mi)
354 ParameterInfo[] p = mi.GetParametersInternal ();
355 sb.Append (mi.DeclaringType.ToString ());
359 if (mi.IsGenericMethod) {
360 Type[] gen_params = mi.GetGenericArguments ();
362 for (int j = 0; j < gen_params.Length; j++) {
365 sb.Append (gen_params [j].Name);
371 for (int i = 0; i < p.Length; ++i) {
374 Type pt = p[i].ParameterType;
375 if (pt.IsClass && !String.IsNullOrEmpty (pt.Namespace)) {
376 sb.Append (pt.Namespace);
380 if (p [i].Name != null) {
382 sb.Append (p [i].Name);
388 // For ExceptionDispatchInfo
389 internal void CaptureTrace ()
391 if (captured_traces != null) {
392 Array.Resize (ref captured_traces, captured_traces.Length + 1);
394 captured_traces = new StackTrace [1];
396 captured_traces [captured_traces.Length - 1] = new StackTrace (this, 0, true, true);
402 // The documentation states that this is available in 1.x,
403 // but it was not available (MemberRefing this would fail)
404 // and it states the signature is `override sealed', but the
405 // correct value is `newslot'
407 public new Type GetType ()
409 return base.GetType ();