2006-05-29 Martin Baulig <martin@ximian.com>
[mono.git] / mcs / class / corlib / System / Exception.cs
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)
10 //
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.
29 //
30
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;
39
40 namespace System
41 {
42         [Serializable]
43         [ClassInterface (ClassInterfaceType.AutoDual)]
44         public class Exception : ISerializable 
45 #if NET_2_0
46         , _Exception
47 #endif
48         {
49                 IntPtr [] trace_ips;
50                 Exception inner_exception;
51                 string message;
52                 string help_link;
53                 string class_name;
54                 string stack_trace;
55                 string remote_stack_trace;
56                 int remote_stack_index;
57                 int hresult = unchecked ((int)0x80004005);
58                 string source;
59
60                 public Exception ()
61                 {
62                 }
63
64                 public Exception (string msg)
65                 {
66                         message = msg;
67                 }
68
69                 protected Exception (SerializationInfo info, StreamingContext sc)
70                 {
71                         if (info == null)
72                                 throw new ArgumentNullException ("info");
73
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));
83                 }
84
85                 public Exception (string msg, Exception e)
86                 {
87                         inner_exception = e;
88                         message = msg;
89                 }
90
91                 public Exception InnerException {
92                         get { return inner_exception; }
93                 }
94
95                 public virtual string HelpLink {
96                         get { return help_link; }
97                         set { help_link = value; }
98                 }
99
100                 protected int HResult {
101                         get { return hresult; }
102                         set { hresult = value; }
103                 }
104
105                 internal void SetMessage (string s)
106                 {
107                         message = s;
108                 }
109
110                 internal void SetStackTrace (string s)
111                 {
112                         stack_trace = s;
113                 }
114
115                 public virtual string Message {
116                         get {
117                                 if (message == null)
118                                         message = string.Format (Locale.GetText ("Exception of type {0} was thrown."), GetType ().ToString());
119
120                                 return message;
121                         }
122                 }
123
124                 public virtual string Source {
125 #if ONLY_1_1
126                         [ReflectionPermission (SecurityAction.Assert, TypeInformation = true)]
127 #endif
128                         get {
129                                 if (source == null) {
130                                         StackTrace st = new StackTrace (this, true);
131                                         if (st.FrameCount > 0) {
132                                                 StackFrame sf = st.GetFrame (0);
133                                                 if (st != null) {
134                                                         MethodBase method = sf.GetMethod ();
135                                                         if (method != null) {
136                                                                 source = method.DeclaringType.Assembly.UnprotectedGetName ().Name;
137                                                         }
138                                                 }
139                                         }
140                                 }
141
142                                 // source can be null
143                                 return source;
144                         }
145
146                         set {
147                                 source = value;
148                         }
149                 }
150
151                 public virtual string StackTrace {
152                         get {
153                                 if (stack_trace == null) {
154                                         if (trace_ips == null)
155                                                 /* Not thrown yet */
156                                                 return null;
157
158                                         StackTrace st = new StackTrace (this, 0, true, true);
159
160                                         StringBuilder sb = new StringBuilder ();
161
162                                         string newline = String.Format ("{0}  {1} ", Environment.NewLine, Locale.GetText ("at"));
163                                         string unknown = Locale.GetText ("<unknown method>");
164
165                                         for (int i = 0; i < st.FrameCount; i++) {
166                                                 StackFrame frame = st.GetFrame (i);
167                                                 if (i == 0)
168                                                         sb.AppendFormat ("  {0} ", Locale.GetText ("at"));
169                                                 else
170                                                         sb.Append (newline);
171
172                                                 if (frame.GetMethod () == null) {
173                                                         string internal_name = frame.GetInternalMethodName ();
174                                                         if (internal_name != null)
175                                                                 sb.Append (internal_name);
176                                                         else
177                                                                 sb.AppendFormat ("<0x{0:x5}> {1}", frame.GetNativeOffset (), unknown);
178                                                 } else {
179                                                         sb.Append (GetFullNameForStackTrace (frame.GetMethod ()));
180
181                                                         if (frame.GetILOffset () == -1)
182                                                                 sb.AppendFormat (" <0x{0:x5}> ", frame.GetNativeOffset ());
183                                                         else
184                                                                 sb.AppendFormat (" [0x{0:x5}] ", frame.GetILOffset ());
185
186                                                         string fileName = frame.GetFileName ();
187                                                         if (fileName != null)
188                                                                 sb.AppendFormat ("in {0}:{1} ", fileName, frame.GetFileLineNumber ());
189                                                         }
190                                         }
191                                         stack_trace = sb.ToString ();
192                                 }
193
194                                 return stack_trace;
195                         }
196                 }
197
198                 public MethodBase TargetSite {
199 #if ONLY_1_1
200                         [ReflectionPermission (SecurityAction.Demand, TypeInformation = true)]
201 #endif
202                         get {
203                                 StackTrace st = new StackTrace (this, true);
204                                 if (st.FrameCount > 0)
205                                         return st.GetFrame (0).GetMethod ();
206                                 
207                                 return null;
208                         }
209                 }
210
211 #if NET_2_0
212                 private IDictionary _data;
213
214                 public virtual IDictionary Data {
215                         get {
216                                 if (_data == null) {
217                                         // default to empty dictionary
218                                         _data = (IDictionary) new Hashtable ();
219                                 }
220                                 return _data;
221                         }
222                 }
223 #endif
224
225                 public virtual Exception GetBaseException ()
226                 {
227                         Exception inner = inner_exception;
228                                 
229                         while (inner != null)
230                         {
231                                 if (inner.InnerException != null)
232                                         inner = inner.InnerException;
233                                 else
234                                         return inner;
235                         }
236
237                         return this;
238                 }
239
240 #if ONLY_1_1
241                 [ReflectionPermission (SecurityAction.Assert, TypeInformation = true)]
242 #endif
243                 [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)]
244                 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
245                 {
246                         if (info == null)
247                                 throw new ArgumentNullException ("info");
248
249                         if (class_name == null)
250                                 class_name = GetType ().FullName;
251
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);
262                 }
263
264 #if ONLY_1_1
265                 [ReflectionPermission (SecurityAction.Assert, TypeInformation = true)]
266 #endif
267                 public override string ToString ()
268                 {
269                         System.Text.StringBuilder result = new System.Text.StringBuilder (this.GetType ().FullName);
270                         result.Append (": ").Append (Message);
271
272                         if (null != remote_stack_trace)
273                                 result.Append (remote_stack_trace);
274                                 
275                         if (inner_exception != null) 
276                         {
277                                 result.Append (" ---> ").Append (inner_exception.ToString ());
278                                 result.Append (Locale.GetText ("--- End of inner exception stack trace ---"));
279                                 result.Append (Environment.NewLine);
280                         }
281
282                         if (StackTrace != null)
283                                 result.Append (Environment.NewLine).Append (StackTrace);
284                         return result.ToString();
285                 }
286
287                 internal Exception FixRemotingException ()
288                 {
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);
293
294                         remote_stack_trace = tmp;
295                         remote_stack_index++;
296
297                         stack_trace = null;
298
299                         return this;
300                 }
301
302                 internal string GetFullNameForStackTrace (MethodBase mi)
303                 {
304                         string parms = "";
305                         ParameterInfo[] p = mi.GetParameters ();
306                         for (int i = 0; i < p.Length; ++i) {
307                                 if (i > 0)
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;
313                                 else
314                                         parms = parms + pt.Name + paramName;
315                         }
316
317                         string generic = "";
318 #if NET_2_0 || BOOTSTRAP_NET_2_0
319                         if (mi.IsGenericMethod) {
320                                 Type[] gen_params = mi.GetGenericArguments ();
321                                 generic = "[";
322                                 for (int j = 0; j < gen_params.Length; j++) {
323                                         if (j > 0)
324                                                 generic += ",";
325                                         generic += gen_params [j].Name;
326                                 }
327                                 generic += "]";
328                         }
329 #endif
330                         return mi.DeclaringType.ToString () + "." + mi.Name + generic + " (" + parms + ")";
331                 }                               
332         }
333 }