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