2005-06-28 Lluis Sanchez Gual <lluis@novell.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 ("in"));
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 ("in"));
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                                                         if (frame.GetILOffset () == -1)
180                                                                 sb.AppendFormat ("<0x{0:x5}> ", frame.GetNativeOffset ());
181                                                         else
182                                                                 sb.AppendFormat ("[0x{0:x5}] ", frame.GetILOffset ());
183                                                         string fileName = frame.GetFileName ();
184                                                         if (fileName != null)
185                                                                 sb.AppendFormat ("(at {0}:{1}) ", fileName, frame.GetFileLineNumber ());
186
187                                                         sb.Append (GetFullNameForStackTrace (frame.GetMethod ()));
188                                                 }
189                                         }
190                                         stack_trace = sb.ToString ();
191                                 }
192
193                                 return stack_trace;
194                         }
195                 }
196
197                 public MethodBase TargetSite {
198 #if ONLY_1_1
199                         [ReflectionPermission (SecurityAction.Demand, TypeInformation = true)]
200 #endif
201                         get {
202                                 StackTrace st = new StackTrace (this, true);
203                                 if (st.FrameCount > 0)
204                                         return st.GetFrame (0).GetMethod ();
205                                 
206                                 return null;
207                         }
208                 }
209
210 #if NET_2_0
211                 private IDictionary _data;
212
213                 public virtual IDictionary Data {
214                         get {
215                                 if (_data == null) {
216                                         // default to empty dictionary
217                                         _data = (IDictionary) new Hashtable ();
218                                 }
219                                 return _data;
220                         }
221                 }
222 #endif
223
224                 public virtual Exception GetBaseException ()
225                 {
226                         Exception inner = inner_exception;
227                                 
228                         while (inner != null)
229                         {
230                                 if (inner.InnerException != null)
231                                         inner = inner.InnerException;
232                                 else
233                                         return inner;
234                         }
235
236                         return this;
237                 }
238
239 #if ONLY_1_1
240                 [ReflectionPermission (SecurityAction.Assert, TypeInformation = true)]
241 #endif
242                 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
243                 {
244                         if (info == null)
245                                 throw new ArgumentNullException ("info");
246
247                         if (class_name == null)
248                                 class_name = GetType ().FullName;
249
250                         info.AddValue ("ClassName", class_name);
251                         info.AddValue ("Message", message);
252                         info.AddValue ("InnerException", inner_exception);
253                         info.AddValue ("HelpURL", help_link);
254                         info.AddValue ("StackTraceString", StackTrace);
255                         info.AddValue ("RemoteStackTraceString", remote_stack_trace);
256                         info.AddValue ("RemoteStackIndex", remote_stack_index);
257                         info.AddValue ("HResult", hresult);
258                         info.AddValue ("Source", Source);
259                         info.AddValue ("ExceptionMethod", null);
260                 }
261
262 #if ONLY_1_1
263                 [ReflectionPermission (SecurityAction.Assert, TypeInformation = true)]
264 #endif
265                 public override string ToString ()
266                 {
267                         System.Text.StringBuilder result = new System.Text.StringBuilder (this.GetType ().FullName);
268                         result.Append (": ").Append (Message);
269
270                         if (null != remote_stack_trace)
271                                 result.Append (remote_stack_trace);
272                                 
273                         if (inner_exception != null) 
274                         {
275                                 result.Append (" ---> ").Append (inner_exception.ToString ());
276                                 result.Append (Locale.GetText ("--- End of inner exception stack trace ---"));
277                                 result.Append (Environment.NewLine);
278                         }
279
280                         if (StackTrace != null)
281                                 result.Append (Environment.NewLine).Append (StackTrace);
282                         return result.ToString();
283                 }
284
285                 internal Exception FixRemotingException ()
286                 {
287                         string message = (0 == remote_stack_index) ?
288                                 Locale.GetText ("{0}{0}Server stack trace: {0}{1}{0}{0}Exception rethrown at [{2}]: {0}") :
289                                 Locale.GetText ("{1}{0}{0}Exception rethrown at [{2}]: {0}");
290                         string tmp = String.Format (message, Environment.NewLine, StackTrace, remote_stack_index);
291
292                         remote_stack_trace = tmp;
293                         remote_stack_index++;
294
295                         stack_trace = null;
296
297                         return this;
298                 }
299
300                 internal string GetFullNameForStackTrace (MethodBase mi)
301                 {
302                         string parms = "";
303                         ParameterInfo[] p = mi.GetParameters ();
304                         for (int i = 0; i < p.Length; ++i) {
305                                 if (i > 0)
306                                         parms = parms + ", ";
307                                 string paramName = (p [i].Name == null) ? "" : (" " + p [i].Name);
308                                 Type pt = p[i].ParameterType;
309                                 if (pt.IsClass && pt.Namespace != "")
310                                         parms = parms + pt.Namespace + "." + pt.Name + paramName;
311                                 else
312                                         parms = parms + pt.Name + paramName;
313                         }
314
315                         string generic = "";
316 #if NET_2_0 || BOOTSTRAP_NET_2_0
317                         if (mi.HasGenericParameters) {
318                                 Type[] gen_params = mi.GetGenericArguments ();
319                                 generic = "[";
320                                 for (int j = 0; j < gen_params.Length; j++) {
321                                         if (j > 0)
322                                                 generic += ",";
323                                         generic += gen_params [j].Name;
324                                 }
325                                 generic += "]";
326                         }
327 #endif
328                         return mi.DeclaringType.ToString () + ":" + mi.Name + generic + " (" + parms + ")";
329                 }                               
330         }
331 }