1 // =================================
 2 // Copyright (c) 2022 Seppo Laakko
 3 // Distributed under the MIT license
 4 // =================================
 5 
 6 namespace System
 7 {
 8     public class Exception
 9     {
10         public nothrow Exception(const string& message_) : message(message_)stackTrace()
11         {
12         }
13         public virtual default ~Exception();
14         public inline nothrow const string& Message() const
15         {
16             return message;
17         }
18         public nothrow const string& StackTrace() const
19         {
20             return stackTrace;
21         }
22         public void SetStackTrace(const string& stackTrace_)
23         {
24             stackTrace = stackTrace_;
25         }
26         public virtual string ToString() const
27         {
28             string s;
29             s.Append(typename(*this)).Append(": ").Append(message).Append('\n').Append(stackTrace);
30             return s;
31         }
32         private string message;
33         private string stackTrace;
34     }
35 
36     public void Throw(Exception* ex)
37     {
38         ex->SetStackTrace(GetStackTrace());
39         if (do_throw(ex) ==           -1)
40         {
41             SystemError systemError = GetSystemError();
42             Console.Error() << systemError.ToString() << endl();
43         }
44     }
45 
46     public class NullPointerException : Exception
47     {
48         public nothrow NullPointerException() : base("null pointer exception")
49         {
50         }
51     }
52 
53     public class IndexOutOfBoundsException : Exception
54     {
55         public nothrow IndexOutOfBoundsException() : base("index out of bounds")
56         {
57         }
58     }
59 
60     public class InvalidParameterException : Exception
61     {
62         public nothrow InvalidParameterException() : base("invalid parameter")
63         {
64         }
65     }
66 
67     public class PreconditionViolationException : Exception
68     {
69         public nothrow PreconditionViolationException() : base("precondition violation")
70         {
71         }
72     }
73 
74     public void ThrowNullPointerException()
75     {
76         throw NullPointerException();
77     }
78 
79     public void ThrowIndexOutOfBoundsException()
80     {
81         throw IndexOutOfBoundsException();
82     }
83 
84     public void ThrowInvalidParameterException()
85     {
86         throw InvalidParameterException();
87     }
88 
89     public void ThrowPreconditionViolationException()
90     {
91         throw PreconditionViolationException();
92     }
93 }