5 // Miguel de Icaza (miguel@ximian.com)
8 // (C) Ximian, Inc. http://www.ximian.com
11 using System.Runtime.InteropServices;
12 using System.Runtime.Serialization;
13 using System.Reflection;
14 using System.Diagnostics;
19 [ClassInterface (ClassInterfaceType.AutoDual)]
20 public class Exception : ISerializable
23 Exception inner_exception;
27 string stack_trace = null;
28 string remote_stack_trace = "";
29 int remote_stack_index = 0;
30 int hresult = unchecked ((int)0x80004005);
35 inner_exception = null;
37 class_name = GetType().FullName;
40 public Exception (string msg)
42 inner_exception = null;
44 class_name = GetType().FullName;
47 protected Exception (SerializationInfo info, StreamingContext sc)
50 throw new ArgumentNullException ("info");
52 class_name = info.GetString ("ClassName");
53 message = info.GetString ("Message");
54 help_link = info.GetString ("HelpURL");
55 stack_trace = info.GetString ("StackTraceString");
56 remote_stack_trace = info.GetString ("RemoteStackTraceString");
57 remote_stack_index = info.GetInt32 ("RemoteStackIndex");
58 hresult = info.GetInt32 ("HResult");
59 source = info.GetString ("Source");
60 inner_exception = (Exception) info.GetValue ("InnerException", typeof (Exception));
63 public Exception (string msg, Exception e)
67 class_name = GetType().FullName;
70 public Exception InnerException {
71 get { return inner_exception; }
74 public virtual string HelpLink {
75 get { return help_link; }
76 set { help_link = value; }
79 protected int HResult {
80 get { return hresult; }
81 set { hresult = value; }
84 internal void SetMessage (string s)
89 public virtual string Message {
92 message = string.Format (Locale.GetText ("Exception of type {0} was thrown."), GetType ().ToString());
98 public virtual string Source {
100 if (source == null) {
101 StackTrace st = new StackTrace (this, true);
102 if (st.FrameCount > 0) {
103 StackFrame sf = st.GetFrame (0);
105 MethodBase method = sf.GetMethod ();
106 if (method != null) {
107 source = method.DeclaringType.Assembly.GetName ().Name;
113 // source can be null
122 public virtual string StackTrace {
128 public MethodBase TargetSite {
130 StackTrace st = new StackTrace (this, true);
131 if (st.FrameCount > 0)
132 return st.GetFrame (0).GetMethod ();
138 public virtual Exception GetBaseException ()
140 Exception inner = inner_exception;
142 while (inner != null)
144 if (inner.InnerException != null)
145 inner = inner.InnerException;
153 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
155 info.AddValue ("ClassName", class_name);
156 info.AddValue ("Message", message);
157 info.AddValue ("InnerException", inner_exception);
158 info.AddValue ("HelpURL", help_link);
159 info.AddValue ("StackTraceString", stack_trace);
160 info.AddValue ("RemoteStackTraceString", remote_stack_trace);
161 info.AddValue ("RemoteStackIndex", remote_stack_index);
162 info.AddValue ("HResult", hresult);
163 info.AddValue ("Source", Source);
164 info.AddValue ("ExceptionMethod", null);
167 public override string ToString ()
169 System.Text.StringBuilder result = new System.Text.StringBuilder (this.GetType ().FullName);
170 result.Append (": ").Append (Message);
172 if (null != remote_stack_trace)
173 result.Append (remote_stack_trace);
175 if (inner_exception != null)
177 result.Append (" ---> ").Append (inner_exception.ToString ());
178 result.Append (Locale.GetText ("--- End of inner exception stack trace ---"));
179 result.Append (Environment.NewLine);
182 if (stack_trace != null)
183 result.Append (Environment.NewLine).Append (stack_trace);
184 return result.ToString();
187 internal Exception FixRemotingException ()
189 string message = (0 == remote_stack_index) ?
190 Locale.GetText ("{0}{0}Server stack trace: {0}{1}{0}{0}Exception rethrown at [{2}]: {0}") :
191 Locale.GetText ("{1}{0}{0}Exception rethrown at [{2}]: {0}");
192 string tmp = String.Format (message, Environment.NewLine, StackTrace, remote_stack_index);
194 remote_stack_trace = tmp;
195 remote_stack_index++;