5 // Miguel de Icaza (miguel@ximian.com)
8 // (C) Ximian, Inc. http://www.ximian.com
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Runtime.InteropServices;
35 using System.Runtime.Serialization;
36 using System.Reflection;
37 using System.Diagnostics;
42 [ClassInterface (ClassInterfaceType.AutoDual)]
43 public class Exception : ISerializable
49 Exception inner_exception;
54 string remote_stack_trace;
55 int remote_stack_index;
56 int hresult = unchecked ((int)0x80004005);
63 public Exception (string msg)
68 protected Exception (SerializationInfo info, StreamingContext sc)
71 throw new ArgumentNullException ("info");
73 class_name = info.GetString ("ClassName");
74 message = info.GetString ("Message");
75 help_link = info.GetString ("HelpURL");
76 stack_trace = info.GetString ("StackTraceString");
77 remote_stack_trace = info.GetString ("RemoteStackTraceString");
78 remote_stack_index = info.GetInt32 ("RemoteStackIndex");
79 hresult = info.GetInt32 ("HResult");
80 source = info.GetString ("Source");
81 inner_exception = (Exception) info.GetValue ("InnerException", typeof (Exception));
84 public Exception (string msg, Exception e)
90 public Exception InnerException {
91 get { return inner_exception; }
94 public virtual string HelpLink {
95 get { return help_link; }
96 set { help_link = value; }
99 protected int HResult {
100 get { return hresult; }
101 set { hresult = value; }
104 internal void SetMessage (string s)
109 internal void SetStackTrace (string s)
114 public virtual string Message {
117 message = string.Format (Locale.GetText ("Exception of type {0} was thrown."), GetType ().ToString());
123 public virtual string Source {
125 if (source == null) {
126 StackTrace st = new StackTrace (this, true);
127 if (st.FrameCount > 0) {
128 StackFrame sf = st.GetFrame (0);
130 MethodBase method = sf.GetMethod ();
131 if (method != null) {
132 source = method.DeclaringType.Assembly.GetName ().Name;
138 // source can be null
147 public virtual string StackTrace {
153 public MethodBase TargetSite {
155 StackTrace st = new StackTrace (this, true);
156 if (st.FrameCount > 0)
157 return st.GetFrame (0).GetMethod ();
163 public virtual Exception GetBaseException ()
165 Exception inner = inner_exception;
167 while (inner != null)
169 if (inner.InnerException != null)
170 inner = inner.InnerException;
178 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
180 if (class_name == null)
181 class_name = GetType ().FullName;
183 info.AddValue ("ClassName", class_name);
184 info.AddValue ("Message", message);
185 info.AddValue ("InnerException", inner_exception);
186 info.AddValue ("HelpURL", help_link);
187 info.AddValue ("StackTraceString", stack_trace);
188 info.AddValue ("RemoteStackTraceString", remote_stack_trace);
189 info.AddValue ("RemoteStackIndex", remote_stack_index);
190 info.AddValue ("HResult", hresult);
191 info.AddValue ("Source", Source);
192 info.AddValue ("ExceptionMethod", null);
195 public override string ToString ()
197 System.Text.StringBuilder result = new System.Text.StringBuilder (this.GetType ().FullName);
198 result.Append (": ").Append (Message);
200 if (null != remote_stack_trace)
201 result.Append (remote_stack_trace);
203 if (inner_exception != null)
205 result.Append (" ---> ").Append (inner_exception.ToString ());
206 result.Append (Locale.GetText ("--- End of inner exception stack trace ---"));
207 result.Append (Environment.NewLine);
210 if (stack_trace != null)
211 result.Append (Environment.NewLine).Append (stack_trace);
212 return result.ToString();
215 internal Exception FixRemotingException ()
217 string message = (0 == remote_stack_index) ?
218 Locale.GetText ("{0}{0}Server stack trace: {0}{1}{0}{0}Exception rethrown at [{2}]: {0}") :
219 Locale.GetText ("{1}{0}{0}Exception rethrown at [{2}]: {0}");
220 string tmp = String.Format (message, Environment.NewLine, StackTrace, remote_stack_index);
222 remote_stack_trace = tmp;
223 remote_stack_index++;