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
46 Exception inner_exception;
50 string stack_trace = null;
51 string remote_stack_trace = "";
52 int remote_stack_index = 0;
53 int hresult = unchecked ((int)0x80004005);
58 inner_exception = null;
60 class_name = GetType().FullName;
63 public Exception (string msg)
65 inner_exception = null;
67 class_name = GetType().FullName;
70 protected Exception (SerializationInfo info, StreamingContext sc)
73 throw new ArgumentNullException ("info");
75 class_name = info.GetString ("ClassName");
76 message = info.GetString ("Message");
77 help_link = info.GetString ("HelpURL");
78 stack_trace = info.GetString ("StackTraceString");
79 remote_stack_trace = info.GetString ("RemoteStackTraceString");
80 remote_stack_index = info.GetInt32 ("RemoteStackIndex");
81 hresult = info.GetInt32 ("HResult");
82 source = info.GetString ("Source");
83 inner_exception = (Exception) info.GetValue ("InnerException", typeof (Exception));
86 public Exception (string msg, Exception e)
90 class_name = GetType().FullName;
93 public Exception InnerException {
94 get { return inner_exception; }
97 public virtual string HelpLink {
98 get { return help_link; }
99 set { help_link = value; }
102 protected int HResult {
103 get { return hresult; }
104 set { hresult = value; }
107 internal void SetMessage (string s)
112 public virtual string Message {
115 message = string.Format (Locale.GetText ("Exception of type {0} was thrown."), GetType ().ToString());
121 public virtual string Source {
123 if (source == null) {
124 StackTrace st = new StackTrace (this, true);
125 if (st.FrameCount > 0) {
126 StackFrame sf = st.GetFrame (0);
128 MethodBase method = sf.GetMethod ();
129 if (method != null) {
130 source = method.DeclaringType.Assembly.GetName ().Name;
136 // source can be null
145 public virtual string StackTrace {
151 public MethodBase TargetSite {
153 StackTrace st = new StackTrace (this, true);
154 if (st.FrameCount > 0)
155 return st.GetFrame (0).GetMethod ();
161 public virtual Exception GetBaseException ()
163 Exception inner = inner_exception;
165 while (inner != null)
167 if (inner.InnerException != null)
168 inner = inner.InnerException;
176 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
178 info.AddValue ("ClassName", class_name);
179 info.AddValue ("Message", message);
180 info.AddValue ("InnerException", inner_exception);
181 info.AddValue ("HelpURL", help_link);
182 info.AddValue ("StackTraceString", stack_trace);
183 info.AddValue ("RemoteStackTraceString", remote_stack_trace);
184 info.AddValue ("RemoteStackIndex", remote_stack_index);
185 info.AddValue ("HResult", hresult);
186 info.AddValue ("Source", Source);
187 info.AddValue ("ExceptionMethod", null);
190 public override string ToString ()
192 System.Text.StringBuilder result = new System.Text.StringBuilder (this.GetType ().FullName);
193 result.Append (": ").Append (Message);
195 if (null != remote_stack_trace)
196 result.Append (remote_stack_trace);
198 if (inner_exception != null)
200 result.Append (" ---> ").Append (inner_exception.ToString ());
201 result.Append (Locale.GetText ("--- End of inner exception stack trace ---"));
202 result.Append (Environment.NewLine);
205 if (stack_trace != null)
206 result.Append (Environment.NewLine).Append (stack_trace);
207 return result.ToString();
210 internal Exception FixRemotingException ()
212 string message = (0 == remote_stack_index) ?
213 Locale.GetText ("{0}{0}Server stack trace: {0}{1}{0}{0}Exception rethrown at [{2}]: {0}") :
214 Locale.GetText ("{1}{0}{0}Exception rethrown at [{2}]: {0}");
215 string tmp = String.Format (message, Environment.NewLine, StackTrace, remote_stack_index);
217 remote_stack_trace = tmp;
218 remote_stack_index++;