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;
43 [ClassInterface (ClassInterfaceType.AutoDual)]
47 public class Exception : ISerializable
53 Exception inner_exception;
54 internal string message;
58 string remote_stack_trace;
59 int remote_stack_index;
60 internal int hresult = unchecked ((int)0x80004005);
67 public Exception (string msg)
72 protected Exception (SerializationInfo info, StreamingContext sc)
75 throw new ArgumentNullException ("info");
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));
88 public Exception (string msg, Exception e)
94 public Exception InnerException {
95 get { return inner_exception; }
98 public virtual string HelpLink {
99 get { return help_link; }
100 set { help_link = value; }
103 protected int HResult {
104 get { return hresult; }
105 set { hresult = value; }
108 internal void SetMessage (string s)
113 internal void SetStackTrace (string s)
118 public virtual string Message {
121 message = string.Format (Locale.GetText ("Exception of type {0} was thrown."), GetType ().ToString());
127 public virtual string Source {
129 [ReflectionPermission (SecurityAction.Assert, TypeInformation = true)]
132 if (source == null) {
133 StackTrace st = new StackTrace (this, true);
134 if (st.FrameCount > 0) {
135 StackFrame sf = st.GetFrame (0);
137 MethodBase method = sf.GetMethod ();
138 if (method != null) {
139 source = method.DeclaringType.Assembly.UnprotectedGetName ().Name;
145 // source can be null
154 public virtual string StackTrace {
156 if (stack_trace == null) {
157 if (trace_ips == null)
161 StackTrace st = new StackTrace (this, 0, true, true);
163 StringBuilder sb = new StringBuilder ();
165 string newline = String.Format ("{0} {1} ", Environment.NewLine, Locale.GetText ("at"));
166 string unknown = Locale.GetText ("<unknown method>");
168 for (int i = 0; i < st.FrameCount; i++) {
169 StackFrame frame = st.GetFrame (i);
171 sb.AppendFormat (" {0} ", Locale.GetText ("at"));
175 if (frame.GetMethod () == null) {
176 string internal_name = frame.GetInternalMethodName ();
177 if (internal_name != null)
178 sb.Append (internal_name);
180 sb.AppendFormat ("<0x{0:x5}> {1}", frame.GetNativeOffset (), unknown);
182 sb.Append (GetFullNameForStackTrace (frame.GetMethod ()));
184 if (frame.GetILOffset () == -1)
185 sb.AppendFormat (" <0x{0:x5}> ", frame.GetNativeOffset ());
187 sb.AppendFormat (" [0x{0:x5}] ", frame.GetILOffset ());
189 string fileName = frame.GetFileName ();
190 if (fileName != null)
191 sb.AppendFormat ("in {0}:{1} ", fileName, frame.GetFileLineNumber ());
194 stack_trace = sb.ToString ();
201 public MethodBase TargetSite {
203 [ReflectionPermission (SecurityAction.Demand, TypeInformation = true)]
206 StackTrace st = new StackTrace (this, true);
207 if (st.FrameCount > 0)
208 return st.GetFrame (0).GetMethod ();
215 private IDictionary _data;
217 public virtual IDictionary Data {
220 // default to empty dictionary
221 _data = (IDictionary) new Hashtable ();
228 public virtual Exception GetBaseException ()
230 Exception inner = inner_exception;
232 while (inner != null)
234 if (inner.InnerException != null)
235 inner = inner.InnerException;
244 [ReflectionPermission (SecurityAction.Assert, TypeInformation = true)]
246 [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)]
247 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
250 throw new ArgumentNullException ("info");
252 if (class_name == null)
253 class_name = GetType ().FullName;
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);
268 [ReflectionPermission (SecurityAction.Assert, TypeInformation = true)]
270 public override string ToString ()
272 System.Text.StringBuilder result = new System.Text.StringBuilder (this.GetType ().FullName);
273 result.Append (": ").Append (Message);
275 if (null != remote_stack_trace)
276 result.Append (remote_stack_trace);
278 if (inner_exception != null)
280 result.Append (" ---> ").Append (inner_exception.ToString ());
281 result.Append (Locale.GetText ("--- End of inner exception stack trace ---"));
282 result.Append (Environment.NewLine);
285 if (StackTrace != null)
286 result.Append (Environment.NewLine).Append (StackTrace);
287 return result.ToString();
290 internal Exception FixRemotingException ()
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);
297 remote_stack_trace = tmp;
298 remote_stack_index++;
305 internal string GetFullNameForStackTrace (MethodBase mi)
307 string parms = String.Empty;
308 ParameterInfo[] p = mi.GetParameters ();
309 for (int i = 0; i < p.Length; ++i) {
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;
317 parms = parms + pt.Name + paramName;
320 string generic = String.Empty;
321 #if NET_2_0 || BOOTSTRAP_NET_2_0
322 if (mi.IsGenericMethod) {
323 Type[] gen_params = mi.GetGenericArguments ();
325 for (int j = 0; j < gen_params.Length; j++) {
328 generic += gen_params [j].Name;
333 return mi.DeclaringType.ToString () + "." + mi.Name + generic + " (" + parms + ")";
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'
343 public new Type GetType ()
345 return base.GetType ();