2002-01-14 Miguel de Icaza <miguel@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 //
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         [MonoTODO]
16         public class Exception : ISerializable {
17                 Exception inner_exception;
18                 string message;
19                 string help_link;
20                 string class_name;
21                 string stack_trace = "TODO: implement stack traces";
22                 int hresult;
23                 private string source;
24                 
25                 public Exception ()
26                 {
27                         inner_exception = null;
28                         message = "";
29                 }
30
31                 public Exception (string msg)
32                 {
33                         inner_exception = null;
34                         message = msg;
35                 }
36
37                 protected Exception (SerializationInfo info, StreamingContext sc)
38                 {
39                         if (info == null)
40                                 throw new ArgumentNullException ("info");
41
42                         class_name      = info.GetString ("ClassName");
43                         message         = info.GetString ("Message");
44                         inner_exception = (Exception) info.GetValue  ("InnerException", typeof (Exception));
45                         help_link       = info.GetString ("HelpURL");
46                         stack_trace     = info.GetString ("StackTraceString");
47                         hresult         = info.GetInt32  ("HResult");
48                 }
49
50                 public Exception (string msg, Exception e)
51                 {
52                         inner_exception = e;
53                         message = msg;
54                 }
55
56                 public Exception InnerException {
57                         get {
58                                 return inner_exception;
59                         }
60                 }
61
62                 public string HelpLink {
63                         get {
64                                 return help_link;
65                         }
66
67                         set {
68                                 help_link = value;
69                         }
70                 }
71
72                 protected int HResult {
73                         get {
74                                 return hresult;
75                         }
76
77                         set {
78                                 hresult = value;
79                         }
80                 }
81
82                 public string Message {
83                         get {
84                                 return message;
85                         }
86                 }
87
88                 [MonoTODO]
89                 public string Source {
90                         get {
91                                 // TODO: if source is null, we must return
92                                 // the name of the assembly where the error
93                                 // originated.
94                                 return source;
95                         }
96
97                         set {
98                                 source = value;
99                         }
100                 }
101
102                 public string StackTrace {
103                         get {
104                                 return stack_trace;
105                         }
106                 }
107
108                 [MonoTODO]
109                 public MethodBase TargetSite {
110                         get {
111                                 // TODO: Implement this.
112                                 return null;
113                         }
114                 }
115
116                 public virtual Exception GetBaseException ()
117                 {
118                         Exception inner = inner_exception;
119                                 
120                         while (inner != null){
121                                 if (inner.InnerException != null)
122                                         inner = inner.InnerException;
123                                 else
124                                         return inner;
125                         }
126
127                         return this;
128                 }
129
130                 [MonoTODO]
131                 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
132                 {
133                         // TODO: implement me.
134                 }
135
136                 public override string ToString ()
137                 {
138                         return this.GetType ().FullName + "\n" +
139                                 message +
140                                 GetBaseException ().GetType ().FullName +
141                                 stack_trace;
142                 }
143         }
144 }
145