This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[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 //
10
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
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:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
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.
32 //
33
34 using System.Runtime.InteropServices;
35 using System.Runtime.Serialization;
36 using System.Reflection;
37 using System.Diagnostics;
38
39 namespace System
40 {
41         [Serializable]
42         [ClassInterface (ClassInterfaceType.AutoDual)]
43         public class Exception : ISerializable 
44         {
45                 IntPtr [] trace_ips;
46                 Exception inner_exception;
47                 string message;
48                 string help_link;
49                 string class_name;
50                 string stack_trace = null;
51                 string remote_stack_trace = "";
52                 int remote_stack_index = 0;
53                 int hresult = unchecked ((int)0x80004005);
54                 string source;
55
56                 public Exception ()
57                 {
58                         inner_exception = null;
59                         message = null;
60                         class_name = GetType().FullName;
61                 }
62
63                 public Exception (string msg)
64                 {
65                         inner_exception = null;
66                         message = msg;
67                         class_name = GetType().FullName;
68                 }
69
70                 protected Exception (SerializationInfo info, StreamingContext sc)
71                 {
72                         if (info == null)
73                                 throw new ArgumentNullException ("info");
74
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));
84                 }
85
86                 public Exception (string msg, Exception e)
87                 {
88                         inner_exception = e;
89                         message = msg;
90                         class_name = GetType().FullName;
91                 }
92
93                 public Exception InnerException {
94                         get { return inner_exception; }
95                 }
96
97                 public virtual string HelpLink {
98                         get { return help_link; }
99                         set { help_link = value; }
100                 }
101
102                 protected int HResult {
103                         get { return hresult; }
104                         set { hresult = value; }
105                 }
106
107                 internal void SetMessage (string s)
108                 {
109                         message = s;
110                 }
111
112                 public virtual string Message {
113                         get {
114                                 if (message == null)
115                                         message = string.Format (Locale.GetText ("Exception of type {0} was thrown."), GetType ().ToString());
116
117                                 return message;
118                         }
119                 }
120
121                 public virtual string Source {
122                         get {
123                                 if (source == null) {
124                                         StackTrace st = new StackTrace (this, true);
125                                         if (st.FrameCount > 0) {
126                                                 StackFrame sf = st.GetFrame (0);
127                                                 if (st != null) {
128                                                         MethodBase method = sf.GetMethod ();
129                                                         if (method != null) {
130                                                                 source = method.DeclaringType.Assembly.GetName ().Name;
131                                                         }
132                                                 }
133                                         }
134                                 }
135
136                                 // source can be null
137                                 return source;
138                         }
139
140                         set {
141                                 source = value;
142                         }
143                 }
144
145                 public virtual string StackTrace {
146                         get {
147                                 return stack_trace;
148                         }
149                 }
150
151                 public MethodBase TargetSite {
152                         get {
153                                 StackTrace st = new StackTrace (this, true);
154                                 if (st.FrameCount > 0)
155                                         return st.GetFrame (0).GetMethod ();
156                                 
157                                 return null;
158                         }
159                 }
160
161                 public virtual Exception GetBaseException ()
162                 {
163                         Exception inner = inner_exception;
164                                 
165                         while (inner != null)
166                         {
167                                 if (inner.InnerException != null)
168                                         inner = inner.InnerException;
169                                 else
170                                         return inner;
171                         }
172
173                         return this;
174                 }
175
176                 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
177                 {
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);
188                 }
189
190                 public override string ToString ()
191                 {
192                         System.Text.StringBuilder result = new System.Text.StringBuilder (this.GetType ().FullName);
193                         result.Append (": ").Append (Message);
194
195                         if (null != remote_stack_trace)
196                                 result.Append (remote_stack_trace);
197                                 
198                         if (inner_exception != null) 
199                         {
200                                 result.Append (" ---> ").Append (inner_exception.ToString ());
201                                 result.Append (Locale.GetText ("--- End of inner exception stack trace ---"));
202                                 result.Append (Environment.NewLine);
203                         }
204
205                         if (stack_trace != null)
206                                 result.Append (Environment.NewLine).Append (stack_trace);
207                         return result.ToString();
208                 }
209
210                 internal Exception FixRemotingException ()
211                 {
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);
216
217                         remote_stack_trace = tmp;
218                         remote_stack_index++;
219
220                         stack_trace = null;
221
222                         return this;
223                 }
224         }
225 }