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 ("at"));
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 ("at"));
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 sb.Append (GetFullNameForStackTrace (frame.GetMethod ()));
181 if (frame.GetILOffset () == -1)
182 sb.AppendFormat (" <0x{0:x5}> ", frame.GetNativeOffset ());
184 sb.AppendFormat (" [0x{0:x5}] ", frame.GetILOffset ());
186 string fileName = frame.GetFileName ();
187 if (fileName != null)
188 sb.AppendFormat ("in {0}:{1} ", fileName, frame.GetFileLineNumber ());
191 stack_trace = sb.ToString ();
198 public MethodBase TargetSite {
200 [ReflectionPermission (SecurityAction.Demand, TypeInformation = true)]
203 StackTrace st = new StackTrace (this, true);
204 if (st.FrameCount > 0)
205 return st.GetFrame (0).GetMethod ();
212 private IDictionary _data;
214 public virtual IDictionary Data {
217 // default to empty dictionary
218 _data = (IDictionary) new Hashtable ();
225 public virtual Exception GetBaseException ()
227 Exception inner = inner_exception;
229 while (inner != null)
231 if (inner.InnerException != null)
232 inner = inner.InnerException;
241 [ReflectionPermission (SecurityAction.Assert, TypeInformation = true)]
243 [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)]
244 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
247 throw new ArgumentNullException ("info");
249 if (class_name == null)
250 class_name = GetType ().FullName;
252 info.AddValue ("ClassName", class_name);
253 info.AddValue ("Message", message);
254 info.AddValue ("InnerException", inner_exception);
255 info.AddValue ("HelpURL", help_link);
256 info.AddValue ("StackTraceString", StackTrace);
257 info.AddValue ("RemoteStackTraceString", remote_stack_trace);
258 info.AddValue ("RemoteStackIndex", remote_stack_index);
259 info.AddValue ("HResult", hresult);
260 info.AddValue ("Source", Source);
261 info.AddValue ("ExceptionMethod", null);
265 [ReflectionPermission (SecurityAction.Assert, TypeInformation = true)]
267 public override string ToString ()
269 System.Text.StringBuilder result = new System.Text.StringBuilder (this.GetType ().FullName);
270 result.Append (": ").Append (Message);
272 if (null != remote_stack_trace)
273 result.Append (remote_stack_trace);
275 if (inner_exception != null)
277 result.Append (" ---> ").Append (inner_exception.ToString ());
278 result.Append (Locale.GetText ("--- End of inner exception stack trace ---"));
279 result.Append (Environment.NewLine);
282 if (StackTrace != null)
283 result.Append (Environment.NewLine).Append (StackTrace);
284 return result.ToString();
287 internal Exception FixRemotingException ()
289 string message = (0 == remote_stack_index) ?
290 Locale.GetText ("{0}{0}Server stack trace: {0}{1}{0}{0}Exception rethrown at [{2}]: {0}") :
291 Locale.GetText ("{1}{0}{0}Exception rethrown at [{2}]: {0}");
292 string tmp = String.Format (message, Environment.NewLine, StackTrace, remote_stack_index);
294 remote_stack_trace = tmp;
295 remote_stack_index++;
302 internal string GetFullNameForStackTrace (MethodBase mi)
305 ParameterInfo[] p = mi.GetParameters ();
306 for (int i = 0; i < p.Length; ++i) {
308 parms = parms + ", ";
309 string paramName = (p [i].Name == null) ? "" : (" " + p [i].Name);
310 Type pt = p[i].ParameterType;
311 if (pt.IsClass && pt.Namespace != "")
312 parms = parms + pt.Namespace + "." + pt.Name + paramName;
314 parms = parms + pt.Name + paramName;
318 #if NET_2_0 || BOOTSTRAP_NET_2_0
319 if (mi.IsGenericMethod) {
320 Type[] gen_params = mi.GetGenericArguments ();
322 for (int j = 0; j < gen_params.Length; j++) {
325 generic += gen_params [j].Name;
330 return mi.DeclaringType.ToString () + "." + mi.Name + generic + " (" + parms + ")";