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.Diagnostics;
33 using System.Reflection;
35 using System.Runtime.InteropServices;
36 using System.Runtime.CompilerServices;
37 using System.Runtime.Serialization;
38 using System.Security.Permissions;
44 [ComDefaultInterface (typeof (_Exception))]
45 [ClassInterface (ClassInterfaceType.None)]
46 public class Exception : ISerializable, _Exception
48 #pragma warning disable 169, 649
49 #region Sync with object-internals.h
50 /* Stores the IPs and the generic sharing infos
51 (vtable/MRGCTX) of the frames. */
53 Exception inner_exception;
54 internal string message;
58 // formerly known as remote_stack_trace (see #425512):
59 string _remoteStackTraceString;
60 int remote_stack_index;
61 internal int hresult = -2146233088;
65 #pragma warning restore 169, 649
71 public Exception (string message)
73 this.message = message;
76 protected Exception (SerializationInfo info, StreamingContext context)
79 throw new ArgumentNullException ("info");
81 class_name = info.GetString ("ClassName");
82 message = info.GetString ("Message");
83 help_link = info.GetString ("HelpURL");
84 stack_trace = info.GetString ("StackTraceString");
85 _remoteStackTraceString = info.GetString ("RemoteStackTraceString");
86 remote_stack_index = info.GetInt32 ("RemoteStackIndex");
87 hresult = info.GetInt32 ("HResult");
88 source = info.GetString ("Source");
89 inner_exception = (Exception) info.GetValue ("InnerException", typeof (Exception));
92 _data = (IDictionary) info.GetValue ("Data", typeof (IDictionary));
93 } catch (SerializationException) {
94 // member did not exist in .NET 1.x
98 public Exception (string message, Exception innerException)
100 inner_exception = innerException;
101 this.message = message;
104 public Exception InnerException {
105 get { return inner_exception; }
108 public virtual string HelpLink {
109 get { return help_link; }
110 set { help_link = value; }
113 protected int HResult {
114 get { return hresult; }
115 set { hresult = value; }
118 internal void SetMessage (string s)
123 internal void SetStackTrace (string s)
130 if (class_name == null)
131 class_name = GetType ().ToString ();
136 public virtual string Message {
139 message = string.Format (Locale.GetText ("Exception of type '{0}' was thrown."),
146 public virtual string Source {
148 if (source == null) {
149 StackTrace st = new StackTrace (this, true);
150 if (st.FrameCount > 0) {
151 StackFrame sf = st.GetFrame (0);
153 MethodBase method = sf.GetMethod ();
154 if (method != null) {
155 source = method.DeclaringType.Assembly.UnprotectedGetName ().Name;
161 // source can be null
170 public virtual string StackTrace {
172 if (stack_trace == null) {
173 if (trace_ips == null)
177 StackTrace st = new StackTrace (this, 0, true, true);
179 StringBuilder sb = new StringBuilder ();
181 string newline = String.Format ("{0} {1} ", Environment.NewLine, Locale.GetText ("at"));
182 string unknown = Locale.GetText ("<unknown method>");
184 for (int i = 0; i < st.FrameCount; i++) {
185 StackFrame frame = st.GetFrame (i);
187 sb.AppendFormat (" {0} ", Locale.GetText ("at"));
191 if (frame.GetMethod () == null) {
192 string internal_name = frame.GetInternalMethodName ();
193 if (internal_name != null)
194 sb.Append (internal_name);
196 sb.AppendFormat ("<0x{0:x5}> {1}", frame.GetNativeOffset (), unknown);
198 GetFullNameForStackTrace (sb, frame.GetMethod ());
200 if (frame.GetILOffset () == -1)
201 sb.AppendFormat (" <0x{0:x5}> ", frame.GetNativeOffset ());
203 sb.AppendFormat (" [0x{0:x5}] ", frame.GetILOffset ());
205 sb.AppendFormat ("in {0}:{1} ", frame.GetSecureFileName (),
206 frame.GetFileLineNumber ());
209 stack_trace = sb.ToString ();
216 public MethodBase TargetSite {
218 StackTrace st = new StackTrace (this, true);
219 if (st.FrameCount > 0)
220 return st.GetFrame (0).GetMethod ();
226 public virtual IDictionary Data {
229 // default to empty dictionary
230 _data = (IDictionary) new Hashtable ();
236 public virtual Exception GetBaseException ()
238 Exception inner = inner_exception;
240 while (inner != null)
242 if (inner.InnerException != null)
243 inner = inner.InnerException;
251 [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)]
252 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
255 throw new ArgumentNullException ("info");
257 info.AddValue ("ClassName", ClassName);
258 info.AddValue ("Message", message);
259 info.AddValue ("InnerException", inner_exception);
260 info.AddValue ("HelpURL", help_link);
261 info.AddValue ("StackTraceString", StackTrace);
262 info.AddValue ("RemoteStackTraceString", _remoteStackTraceString);
263 info.AddValue ("RemoteStackIndex", remote_stack_index);
264 info.AddValue ("HResult", hresult);
266 info.AddValue ("Source", Source);
268 info.AddValue ("Source", null);
270 info.AddValue ("ExceptionMethod", null);
271 info.AddValue ("Data", _data, typeof (IDictionary));
274 public override string ToString ()
276 System.Text.StringBuilder result = new System.Text.StringBuilder (ClassName);
277 result.Append (": ").Append (Message);
279 if (null != _remoteStackTraceString)
280 result.Append (_remoteStackTraceString);
282 if (inner_exception != null)
284 result.Append (" ---> ").Append (inner_exception.ToString ());
285 result.Append (Environment.NewLine);
286 result.Append (Locale.GetText (" --- End of inner exception stack trace ---"));
289 if (StackTrace != null)
290 result.Append (Environment.NewLine).Append (StackTrace);
291 return result.ToString();
294 internal Exception FixRemotingException ()
296 string message = (0 == remote_stack_index) ?
297 Locale.GetText ("{0}{0}Server stack trace: {0}{1}{0}{0}Exception rethrown at [{2}]: {0}") :
298 Locale.GetText ("{1}{0}{0}Exception rethrown at [{2}]: {0}");
299 string tmp = String.Format (message, Environment.NewLine, StackTrace, remote_stack_index);
301 _remoteStackTraceString = tmp;
302 remote_stack_index++;
309 internal void GetFullNameForStackTrace (StringBuilder sb, MethodBase mi)
311 ParameterInfo[] p = mi.GetParameters ();
312 sb.Append (mi.DeclaringType.ToString ());
316 if (mi.IsGenericMethod) {
317 Type[] gen_params = mi.GetGenericArguments ();
319 for (int j = 0; j < gen_params.Length; j++) {
322 sb.Append (gen_params [j].Name);
328 for (int i = 0; i < p.Length; ++i) {
331 Type pt = p[i].ParameterType;
332 if (pt.IsClass && pt.Namespace != String.Empty) {
333 sb.Append (pt.Namespace);
337 if (p [i].Name != null) {
339 sb.Append (p [i].Name);
346 // The documentation states that this is available in 1.x,
347 // but it was not available (MemberRefing this would fail)
348 // and it states the signature is `override sealed', but the
349 // correct value is `newslot'
351 public new Type GetType ()
353 return base.GetType ();