* Exception.cs (Source): This can return null.
[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 using System.Runtime.InteropServices;
12 using System.Runtime.Serialization;
13 using System.Reflection;
14 using System.Diagnostics;
15
16 namespace System
17 {
18         [Serializable]
19         [ClassInterface (ClassInterfaceType.AutoDual)]
20         public class Exception : ISerializable 
21         {
22                 IntPtr [] trace_ips;
23                 Exception inner_exception;
24                 string message;
25                 string help_link;
26                 string class_name;
27                 string stack_trace = null;
28                 string remote_stack_trace = "";
29                 int remote_stack_index = 0;
30                 int hresult = unchecked ((int)0x80004005);
31                 string source;
32
33                 public Exception ()
34                 {
35                         inner_exception = null;
36                         message = null;
37                         class_name = GetType().FullName;
38                 }
39
40                 public Exception (string msg)
41                 {
42                         inner_exception = null;
43                         message = msg;
44                         class_name = GetType().FullName;
45                 }
46
47                 protected Exception (SerializationInfo info, StreamingContext sc)
48                 {
49                         if (info == null)
50                                 throw new ArgumentNullException ("info");
51
52                         class_name          = info.GetString ("ClassName");
53                         message             = info.GetString ("Message");
54                         help_link           = info.GetString ("HelpURL");
55                         stack_trace         = info.GetString ("StackTraceString");
56                         remote_stack_trace  = info.GetString ("RemoteStackTraceString");
57                         remote_stack_index  = info.GetInt32  ("RemoteStackIndex");
58                         hresult             = info.GetInt32  ("HResult");
59                         source              = info.GetString ("Source");
60                         inner_exception     = (Exception) info.GetValue ("InnerException", typeof (Exception));
61                 }
62
63                 public Exception (string msg, Exception e)
64                 {
65                         inner_exception = e;
66                         message = msg;
67                         class_name = GetType().FullName;
68                 }
69
70                 public Exception InnerException {
71                         get { return inner_exception; }
72                 }
73
74                 public virtual string HelpLink {
75                         get { return help_link; }
76                         set { help_link = value; }
77                 }
78
79                 protected int HResult {
80                         get { return hresult; }
81                         set { hresult = value; }
82                 }
83
84                 internal void SetMessage (string s)
85                 {
86                         message = s;
87                 }
88
89                 public virtual string Message {
90                         get {
91                                 if (message == null)
92                                         message = string.Format (Locale.GetText ("Exception of type {0} was thrown."), GetType ().ToString());
93
94                                 return message;
95                         }
96                 }
97
98                 public virtual string Source {
99                         get {
100                                 if (source == null) {
101                                         StackTrace st = new StackTrace (this, true);
102                                         if (st.FrameCount > 0) {
103                                                 StackFrame sf = st.GetFrame (0);
104                                                 if (st != null) {
105                                                         MethodBase method = sf.GetMethod ();
106                                                         if (method != null) {
107                                                                 source = method.DeclaringType.Assembly.GetName ().Name;
108                                                         }
109                                                 }
110                                         }
111                                 }
112
113                                 // source can be null
114                                 return source;
115                         }
116
117                         set {
118                                 source = value;
119                         }
120                 }
121
122                 public virtual string StackTrace {
123                         get {
124                                 return stack_trace;
125                         }
126                 }
127
128                 public MethodBase TargetSite {
129                         get {
130                                 StackTrace st = new StackTrace (this, true);
131                                 if (st.FrameCount > 0)
132                                         return st.GetFrame (0).GetMethod ();
133                                 
134                                 return null;
135                         }
136                 }
137
138                 public virtual Exception GetBaseException ()
139                 {
140                         Exception inner = inner_exception;
141                                 
142                         while (inner != null)
143                         {
144                                 if (inner.InnerException != null)
145                                         inner = inner.InnerException;
146                                 else
147                                         return inner;
148                         }
149
150                         return this;
151                 }
152
153                 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
154                 {
155                         info.AddValue ("ClassName", class_name);
156                         info.AddValue ("Message", message);
157                         info.AddValue ("InnerException", inner_exception);
158                         info.AddValue ("HelpURL", help_link);
159                         info.AddValue ("StackTraceString", stack_trace);
160                         info.AddValue ("RemoteStackTraceString", remote_stack_trace);
161                         info.AddValue ("RemoteStackIndex", remote_stack_index);
162                         info.AddValue ("HResult", hresult);
163                         info.AddValue ("Source", Source);
164                         info.AddValue ("ExceptionMethod", null);
165                 }
166
167                 public override string ToString ()
168                 {
169                         System.Text.StringBuilder result = new System.Text.StringBuilder (this.GetType ().FullName);
170                         result.Append (": ").Append (Message);
171
172                         if (null != remote_stack_trace)
173                                 result.Append (remote_stack_trace);
174                                 
175                         if (inner_exception != null) 
176                         {
177                                 result.Append (" ---> ").Append (inner_exception.ToString ());
178                                 result.Append (Locale.GetText ("--- End of inner exception stack trace ---"));
179                                 result.Append (Environment.NewLine);
180                         }
181
182                         if (stack_trace != null)
183                                 result.Append (Environment.NewLine).Append (stack_trace);
184                         return result.ToString();
185                 }
186
187                 internal Exception FixRemotingException ()
188                 {
189                         string message = (0 == remote_stack_index) ?
190                                 Locale.GetText ("{0}{0}Server stack trace: {0}{1}{0}{0}Exception rethrown at [{2}]: {0}") :
191                                 Locale.GetText ("{1}{0}{0}Exception rethrown at [{2}]: {0}");
192                         string tmp = String.Format (message, Environment.NewLine, StackTrace, remote_stack_index);
193
194                         remote_stack_trace = tmp;
195                         remote_stack_index++;
196
197                         stack_trace = null;
198
199                         return this;
200                 }
201         }
202 }