2010-06-04 Jb Evain <jbevain@novell.com>
[mcs.git] / class / corlib / System / Exception.cs
blob01a2a5b134a6dc11af6cffd96ef41fadeea9ec9c
1 //
2 // System.Exception.cs
3 //
4 // Author:
5 // Miguel de Icaza (miguel@ximian.com)
6 // Patrik Torstensson
7 //
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:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
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;
34 using System.Text;
35 using System.Runtime.InteropServices;
36 using System.Runtime.CompilerServices;
37 using System.Runtime.Serialization;
38 using System.Security.Permissions;
40 namespace System
42 [Serializable]
43 [ComVisible(true)]
44 [ComDefaultInterface (typeof (_Exception))]
45 [ClassInterface (ClassInterfaceType.None)]
46 public class Exception : ISerializable, _Exception
48 #pragma warning disable 169, 649
49 #region Sync with object-internals.h
50 /* Stores the IPs and the generic sharing infos
51 (vtable/MRGCTX) of the frames. */
52 IntPtr [] trace_ips;
53 Exception inner_exception;
54 internal string message;
55 string help_link;
56 string class_name;
57 string stack_trace;
58 // formerly known as remote_stack_trace (see #425512):
59 string _remoteStackTraceString;
60 int remote_stack_index;
61 internal int hresult = -2146233088;
62 string source;
63 IDictionary _data;
64 #endregion
65 #pragma warning restore 169, 649
67 #if NET_4_0
68 protected event EventHandler<SafeSerializationEventArgs> SerializeObjectState {
69 [MonoTODO]
70 add { throw new NotImplementedException (); }
71 [MonoTODO]
72 remove { throw new NotImplementedException (); }
74 #endif
76 public Exception ()
80 public Exception (string message)
82 this.message = message;
85 protected Exception (SerializationInfo info, StreamingContext context)
87 if (info == null)
88 throw new ArgumentNullException ("info");
90 class_name = info.GetString ("ClassName");
91 message = info.GetString ("Message");
92 help_link = info.GetString ("HelpURL");
93 stack_trace = info.GetString ("StackTraceString");
94 _remoteStackTraceString = info.GetString ("RemoteStackTraceString");
95 remote_stack_index = info.GetInt32 ("RemoteStackIndex");
96 hresult = info.GetInt32 ("HResult");
97 source = info.GetString ("Source");
98 inner_exception = (Exception) info.GetValue ("InnerException", typeof (Exception));
100 try {
101 _data = (IDictionary) info.GetValue ("Data", typeof (IDictionary));
102 } catch (SerializationException) {
103 // member did not exist in .NET 1.x
107 public Exception (string message, Exception innerException)
109 inner_exception = innerException;
110 this.message = message;
113 public Exception InnerException {
114 get { return inner_exception; }
117 public virtual string HelpLink {
118 get { return help_link; }
119 set { help_link = value; }
122 protected int HResult {
123 get { return hresult; }
124 set { hresult = value; }
127 internal void SetMessage (string s)
129 message = s;
132 internal void SetStackTrace (string s)
134 stack_trace = s;
137 string ClassName {
138 get {
139 if (class_name == null)
140 class_name = GetType ().ToString ();
141 return class_name;
145 public virtual string Message {
146 get {
147 if (message == null)
148 message = string.Format (Locale.GetText ("Exception of type '{0}' was thrown."),
149 ClassName);
151 return message;
155 public virtual string Source {
156 get {
157 if (source == null) {
158 StackTrace st = new StackTrace (this, true);
159 if (st.FrameCount > 0) {
160 StackFrame sf = st.GetFrame (0);
161 if (st != null) {
162 MethodBase method = sf.GetMethod ();
163 if (method != null) {
164 source = method.DeclaringType.Assembly.UnprotectedGetName ().Name;
170 // source can be null
171 return source;
174 set {
175 source = value;
179 public virtual string StackTrace {
180 get {
181 if (stack_trace == null) {
182 if (trace_ips == null)
183 /* Not thrown yet */
184 return null;
186 StackTrace st = new StackTrace (this, 0, true, true);
188 StringBuilder sb = new StringBuilder ();
190 string newline = String.Format ("{0} {1} ", Environment.NewLine, Locale.GetText ("at"));
191 string unknown = Locale.GetText ("<unknown method>");
193 for (int i = 0; i < st.FrameCount; i++) {
194 StackFrame frame = st.GetFrame (i);
195 if (i == 0)
196 sb.AppendFormat (" {0} ", Locale.GetText ("at"));
197 else
198 sb.Append (newline);
200 if (frame.GetMethod () == null) {
201 string internal_name = frame.GetInternalMethodName ();
202 if (internal_name != null)
203 sb.Append (internal_name);
204 else
205 sb.AppendFormat ("<0x{0:x5}> {1}", frame.GetNativeOffset (), unknown);
206 } else {
207 GetFullNameForStackTrace (sb, frame.GetMethod ());
209 if (frame.GetILOffset () == -1)
210 sb.AppendFormat (" <0x{0:x5}> ", frame.GetNativeOffset ());
211 else
212 sb.AppendFormat (" [0x{0:x5}] ", frame.GetILOffset ());
214 sb.AppendFormat ("in {0}:{1} ", frame.GetSecureFileName (),
215 frame.GetFileLineNumber ());
218 stack_trace = sb.ToString ();
221 return stack_trace;
225 public MethodBase TargetSite {
226 get {
227 StackTrace st = new StackTrace (this, true);
228 if (st.FrameCount > 0)
229 return st.GetFrame (0).GetMethod ();
231 return null;
235 public virtual IDictionary Data {
236 get {
237 if (_data == null) {
238 // default to empty dictionary
239 _data = (IDictionary) new Hashtable ();
241 return _data;
245 public virtual Exception GetBaseException ()
247 Exception inner = inner_exception;
249 while (inner != null)
251 if (inner.InnerException != null)
252 inner = inner.InnerException;
253 else
254 return inner;
257 return this;
260 [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)]
261 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
263 if (info == null)
264 throw new ArgumentNullException ("info");
266 info.AddValue ("ClassName", ClassName);
267 info.AddValue ("Message", message);
268 info.AddValue ("InnerException", inner_exception);
269 info.AddValue ("HelpURL", help_link);
270 info.AddValue ("StackTraceString", StackTrace);
271 info.AddValue ("RemoteStackTraceString", _remoteStackTraceString);
272 info.AddValue ("RemoteStackIndex", remote_stack_index);
273 info.AddValue ("HResult", hresult);
274 #if !MOONLIGHT
275 info.AddValue ("Source", Source);
276 #else
277 info.AddValue ("Source", null);
278 #endif
279 info.AddValue ("ExceptionMethod", null);
280 info.AddValue ("Data", _data, typeof (IDictionary));
283 public override string ToString ()
285 System.Text.StringBuilder result = new System.Text.StringBuilder (ClassName);
286 result.Append (": ").Append (Message);
288 if (null != _remoteStackTraceString)
289 result.Append (_remoteStackTraceString);
291 if (inner_exception != null)
293 result.Append (" ---> ").Append (inner_exception.ToString ());
294 result.Append (Environment.NewLine);
295 result.Append (Locale.GetText (" --- End of inner exception stack trace ---"));
298 if (StackTrace != null)
299 result.Append (Environment.NewLine).Append (StackTrace);
300 return result.ToString();
303 internal Exception FixRemotingException ()
305 string message = (0 == remote_stack_index) ?
306 Locale.GetText ("{0}{0}Server stack trace: {0}{1}{0}{0}Exception rethrown at [{2}]: {0}") :
307 Locale.GetText ("{1}{0}{0}Exception rethrown at [{2}]: {0}");
308 string tmp = String.Format (message, Environment.NewLine, StackTrace, remote_stack_index);
310 _remoteStackTraceString = tmp;
311 remote_stack_index++;
313 stack_trace = null;
315 return this;
318 internal void GetFullNameForStackTrace (StringBuilder sb, MethodBase mi)
320 ParameterInfo[] p = mi.GetParameters ();
321 sb.Append (mi.DeclaringType.ToString ());
322 sb.Append (".");
323 sb.Append (mi.Name);
325 if (mi.IsGenericMethod) {
326 Type[] gen_params = mi.GetGenericArguments ();
327 sb.Append ("[");
328 for (int j = 0; j < gen_params.Length; j++) {
329 if (j > 0)
330 sb.Append (",");
331 sb.Append (gen_params [j].Name);
333 sb.Append ("]");
336 sb.Append (" (");
337 for (int i = 0; i < p.Length; ++i) {
338 if (i > 0)
339 sb.Append (", ");
340 Type pt = p[i].ParameterType;
341 if (pt.IsClass && pt.Namespace != String.Empty) {
342 sb.Append (pt.Namespace);
343 sb.Append (".");
345 sb.Append (pt.Name);
346 if (p [i].Name != null) {
347 sb.Append (" ");
348 sb.Append (p [i].Name);
351 sb.Append (")");
355 // The documentation states that this is available in 1.x,
356 // but it was not available (MemberRefing this would fail)
357 // and it states the signature is `override sealed', but the
358 // correct value is `newslot'
360 public new Type GetType ()
362 return base.GetType ();