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)]
44 public class Exception : ISerializable
50 Exception inner_exception;
55 string remote_stack_trace;
56 int remote_stack_index;
57 int hresult = unchecked ((int)0x80004005);
64 public Exception (string msg)
69 protected Exception (SerializationInfo info, StreamingContext sc)
72 throw new ArgumentNullException ("info");
74 class_name = info.GetString ("ClassName");
75 message = info.GetString ("Message");
76 help_link = info.GetString ("HelpURL");
77 stack_trace = info.GetString ("StackTraceString");
78 remote_stack_trace = info.GetString ("RemoteStackTraceString");
79 remote_stack_index = info.GetInt32 ("RemoteStackIndex");
80 hresult = info.GetInt32 ("HResult");
81 source = info.GetString ("Source");
82 inner_exception = (Exception) info.GetValue ("InnerException", typeof (Exception));
85 public Exception (string msg, Exception e)
91 public Exception InnerException {
92 get { return inner_exception; }
95 public virtual string HelpLink {
96 get { return help_link; }
97 set { help_link = value; }
100 protected int HResult {
101 get { return hresult; }
102 set { hresult = value; }
105 internal void SetMessage (string s)
110 internal void SetStackTrace (string s)
115 public virtual string Message {
118 message = string.Format (Locale.GetText ("Exception of type {0} was thrown."), GetType ().ToString());
124 public virtual string Source {
126 [ReflectionPermission (SecurityAction.Assert, TypeInformation = true)]
129 if (source == null) {
130 StackTrace st = new StackTrace (this, true);
131 if (st.FrameCount > 0) {
132 StackFrame sf = st.GetFrame (0);
134 MethodBase method = sf.GetMethod ();
135 if (method != null) {
136 source = method.DeclaringType.Assembly.UnprotectedGetName ().Name;
142 // source can be null
151 public virtual string StackTrace {
153 if (stack_trace == null) {
154 if (trace_ips == null)
158 StackTrace st = new StackTrace (this, 0, true, true);
160 StringBuilder sb = new StringBuilder ();
162 string newline = String.Format ("{0}{1} ", Environment.NewLine, Locale.GetText ("in"));
163 string unknown = Locale.GetText ("<unknown method>");
165 for (int i = 0; i < st.FrameCount; i++) {
166 StackFrame frame = st.GetFrame (i);
168 sb.AppendFormat ("{0} ", Locale.GetText ("in"));
172 if (frame.GetMethod () == null) {
173 string internal_name = frame.GetInternalMethodName ();
174 if (internal_name != null)
175 sb.Append (internal_name);
177 sb.AppendFormat ("<0x{0:x5}> {1}", frame.GetNativeOffset (), unknown);
179 if (frame.GetILOffset () == -1)
180 sb.AppendFormat ("<0x{0:x5}> ", frame.GetNativeOffset ());
182 sb.AppendFormat ("[0x{0:x5}] ", frame.GetILOffset ());
183 string fileName = frame.GetFileName ();
184 if (fileName != null)
185 sb.AppendFormat ("(at {0}:{1}) ", fileName, frame.GetFileLineNumber ());
187 sb.Append (GetFullNameForStackTrace (frame.GetMethod ()));
190 stack_trace = sb.ToString ();
197 public MethodBase TargetSite {
199 [ReflectionPermission (SecurityAction.Demand, TypeInformation = true)]
202 StackTrace st = new StackTrace (this, true);
203 if (st.FrameCount > 0)
204 return st.GetFrame (0).GetMethod ();
211 private IDictionary _data;
213 public virtual IDictionary Data {
216 // default to empty dictionary
217 _data = (IDictionary) new Hashtable ();
224 public virtual Exception GetBaseException ()
226 Exception inner = inner_exception;
228 while (inner != null)
230 if (inner.InnerException != null)
231 inner = inner.InnerException;
240 [ReflectionPermission (SecurityAction.Assert, TypeInformation = true)]
242 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
245 throw new ArgumentNullException ("info");
247 if (class_name == null)
248 class_name = GetType ().FullName;
250 info.AddValue ("ClassName", class_name);
251 info.AddValue ("Message", message);
252 info.AddValue ("InnerException", inner_exception);
253 info.AddValue ("HelpURL", help_link);
254 info.AddValue ("StackTraceString", StackTrace);
255 info.AddValue ("RemoteStackTraceString", remote_stack_trace);
256 info.AddValue ("RemoteStackIndex", remote_stack_index);
257 info.AddValue ("HResult", hresult);
258 info.AddValue ("Source", Source);
259 info.AddValue ("ExceptionMethod", null);
263 [ReflectionPermission (SecurityAction.Assert, TypeInformation = true)]
265 public override string ToString ()
267 System.Text.StringBuilder result = new System.Text.StringBuilder (this.GetType ().FullName);
268 result.Append (": ").Append (Message);
270 if (null != remote_stack_trace)
271 result.Append (remote_stack_trace);
273 if (inner_exception != null)
275 result.Append (" ---> ").Append (inner_exception.ToString ());
276 result.Append (Locale.GetText ("--- End of inner exception stack trace ---"));
277 result.Append (Environment.NewLine);
280 if (StackTrace != null)
281 result.Append (Environment.NewLine).Append (StackTrace);
282 return result.ToString();
285 internal Exception FixRemotingException ()
287 string message = (0 == remote_stack_index) ?
288 Locale.GetText ("{0}{0}Server stack trace: {0}{1}{0}{0}Exception rethrown at [{2}]: {0}") :
289 Locale.GetText ("{1}{0}{0}Exception rethrown at [{2}]: {0}");
290 string tmp = String.Format (message, Environment.NewLine, StackTrace, remote_stack_index);
292 remote_stack_trace = tmp;
293 remote_stack_index++;
300 internal string GetFullNameForStackTrace (MethodBase mi)
303 ParameterInfo[] p = mi.GetParameters ();
304 for (int i = 0; i < p.Length; ++i) {
306 parms = parms + ", ";
307 string paramName = (p [i].Name == null) ? "" : (" " + p [i].Name);
308 Type pt = p[i].ParameterType;
309 if (pt.IsClass && pt.Namespace != "")
310 parms = parms + pt.Namespace + "." + pt.Name + paramName;
312 parms = parms + pt.Name + paramName;
316 #if NET_2_0 || BOOTSTRAP_NET_2_0
317 if (mi.HasGenericParameters) {
318 Type[] gen_params = mi.GetGenericArguments ();
320 for (int j = 0; j < gen_params.Length; j++) {
323 generic += gen_params [j].Name;
328 return mi.DeclaringType.ToString () + ":" + mi.Name + generic + " (" + parms + ")";