74a26ac66bc03b1b94c10ce87a94547a9a1a67b5
[mono.git] / mcs / class / corlib / System / Exception.cs
1 //
2 // System.Exception.cs
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //
7 // (C) Ximian, Inc.  http://www.ximian.com
8 //
9
10 using System.Runtime.Serialization;
11 using System.Reflection;
12
13 namespace System {
14
15         [Serializable]
16 //      [ClassInterface (ClassInterfaceType.AutoDual)] (no implementation yet)
17         [MonoTODO]
18         public class Exception : ISerializable {
19                 Exception inner_exception;
20                 string message;
21                 string help_link;
22                 string class_name;
23                 string stack_trace = "TODO: implement stack traces";
24                 string remote_stack_trace = "TODO: Implement remote stack trace";
25                 int remote_stack_index;
26                 int hresult;
27                 string source;
28                 
29                 public Exception ()
30                 {
31                         inner_exception = null;
32                         message = "";
33                 }
34
35                 public Exception (string msg)
36                 {
37                         inner_exception = null;
38                         message = msg;
39                 }
40
41                 protected Exception (SerializationInfo info, StreamingContext sc)
42                 {
43                         if (info == null)
44                                 throw new ArgumentNullException ("info");
45
46                         class_name      = info.GetString ("ClassName");
47                         message         = info.GetString ("Message");
48                         inner_exception = (Exception) info.GetValue  ("InnerException", typeof (Exception));
49                         help_link       = info.GetString ("HelpURL");
50                         stack_trace     = info.GetString ("StackTraceString");
51                         remote_stack_trace = info.GetString ("RemoteStackTrace");
52                         remote_stack_index = info.GetInt32  ("RemoteStackIndex");
53                         hresult            = info.GetInt32  ("HResult");
54                         source             = info.GetString ("Source");
55                 }
56
57                 public Exception (string msg, Exception e)
58                 {
59                         inner_exception = e;
60                         message = msg;
61                 }
62
63                 public Exception InnerException {
64                         get {
65                                 return inner_exception;
66                         }
67                 }
68
69                 public virtual string HelpLink {
70                         get {
71                                 return help_link;
72                         }
73
74                         set {
75                                 help_link = value;
76                         }
77                 }
78
79                 protected int HResult {
80                         get {
81                                 return hresult;
82                         }
83
84                         set {
85                                 hresult = value;
86                         }
87                 }
88
89                 public virtual string Message {
90                         get {
91                                 return message;
92                         }
93                 }
94
95                 [MonoTODO]
96                 public virtual string Source {
97                         get {
98                                 // TODO: if source is null, we must return
99                                 // the name of the assembly where the error
100                                 // originated.
101                                 return source;
102                         }
103
104                         set {
105                                 source = value;
106                         }
107                 }
108
109                 public virtual string StackTrace {
110                         get {
111                                 return stack_trace;
112                         }
113                 }
114
115                 [MonoTODO]
116                 public MethodBase TargetSite {
117                         get {
118                                 // TODO: Implement this.
119                                 return null;
120                         }
121                 }
122
123                 public virtual Exception GetBaseException ()
124                 {
125                         Exception inner = inner_exception;
126                                 
127                         while (inner != null){
128                                 if (inner.InnerException != null)
129                                         inner = inner.InnerException;
130                                 else
131                                         return inner;
132                         }
133
134                         return this;
135                 }
136
137                 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
138                 {
139                         info.AddValue ("ClassName", class_name);
140                         info.AddValue ("Message",   message);
141                         info.AddValue  ("InnerException", inner_exception);
142                         info.AddValue ("HelpURL",   help_link);
143                         info.AddValue ("StackTraceString", stack_trace);
144                         info.AddValue ("RemoteStackTrace", remote_stack_trace);
145                         info.AddValue ("RemoteStackIndex", remote_stack_index);
146                         info.AddValue ("HResult", hresult);
147                         info.AddValue ("Source", source);
148                 }
149
150                 public override string ToString ()
151                 {
152                         return this.GetType ().FullName + "\n" +
153                                 message +
154                                 GetBaseException ().GetType ().FullName +
155                                 stack_trace;
156                 }
157         }
158 }
159