1 | // This may look like C code, but it is really -*- C++ -*-
|
---|
2 | #ifndef PEXCEPTIONS_SEEN
|
---|
3 | #define PEXCEPTIONS_SEEN
|
---|
4 |
|
---|
5 | #include "machdefs.h"
|
---|
6 | #include <string>
|
---|
7 |
|
---|
8 | using namespace std;
|
---|
9 |
|
---|
10 | namespace SOPHYA {
|
---|
11 |
|
---|
12 | // Ancestor for PError and PException
|
---|
13 | // It has a message, and an id to give more
|
---|
14 | // information on the exception.
|
---|
15 | class PThrowable {
|
---|
16 | public:
|
---|
17 | explicit PThrowable(const string& m, int ident=0)
|
---|
18 | : msg(m), id(ident) {}
|
---|
19 | virtual ~PThrowable() { }
|
---|
20 | virtual string const& Msg() const {return msg;}
|
---|
21 | private:
|
---|
22 | string msg;
|
---|
23 | int id;
|
---|
24 | };
|
---|
25 |
|
---|
26 | // A PError is a serious logic error. Usually not caught...
|
---|
27 | class PError : public PThrowable {
|
---|
28 | public:
|
---|
29 | explicit PError(const string& m, int id=0) : PThrowable(m,id) {}
|
---|
30 | };
|
---|
31 |
|
---|
32 | // A PException is not as serious... Can be caught.
|
---|
33 | class PException : public PThrowable {
|
---|
34 | public:
|
---|
35 | explicit PException(const string& m, int id=0) : PThrowable(m,id) {}
|
---|
36 | };
|
---|
37 |
|
---|
38 | // Errors
|
---|
39 | // Memory allocation failure
|
---|
40 | class AllocationError : public PError {
|
---|
41 | public:
|
---|
42 | explicit AllocationError(const string& m, int id=0) : PError(m,id) {}
|
---|
43 | };
|
---|
44 |
|
---|
45 | // Null pointer error
|
---|
46 | class NullPtrError : public PError {
|
---|
47 | public:
|
---|
48 | explicit NullPtrError(const string& m, int id=0) : PError(m,id) {}
|
---|
49 | };
|
---|
50 |
|
---|
51 | // Size mismatch between objects
|
---|
52 | class SzMismatchError : public PError {
|
---|
53 | public:
|
---|
54 | explicit SzMismatchError(const string& m, int id=0) : PError(m,id) {}
|
---|
55 | };
|
---|
56 |
|
---|
57 | // Out of bounds for array, matrix, etc.
|
---|
58 | class RangeCheckError : public PError {
|
---|
59 | public:
|
---|
60 | explicit RangeCheckError(const string& m, int id=0) : PError(m,id) {}
|
---|
61 | };
|
---|
62 |
|
---|
63 | // Invalid parameter to method/constructor...
|
---|
64 | class ParmError : public PError {
|
---|
65 | public:
|
---|
66 | explicit ParmError(const string& m, int id=0) : PError(m,id) {}
|
---|
67 | };
|
---|
68 |
|
---|
69 | // Calling a forbidden method, trying a forbidden operation
|
---|
70 | class ForbiddenError : public PError {
|
---|
71 | public:
|
---|
72 | explicit ForbiddenError(const string& m, int id=0) : PError(m,id) {}
|
---|
73 | };
|
---|
74 |
|
---|
75 | // Calling a non available / not implemented method
|
---|
76 | class NotAvailableOperation : public PException {
|
---|
77 | public:
|
---|
78 | explicit NotAvailableOperation(const string& m, int id=0) : PException(m,id) {}
|
---|
79 | };
|
---|
80 |
|
---|
81 | // ASSERT macro failure. The message is the assertion...
|
---|
82 | class AssertionFailedError : public PError {
|
---|
83 | public:
|
---|
84 | explicit AssertionFailedError(const string& m, int id=0) : PError(m,id) {}
|
---|
85 | };
|
---|
86 |
|
---|
87 | // Standard exceptions
|
---|
88 | //
|
---|
89 | // PException
|
---|
90 | // IOExc
|
---|
91 | // FileFormatExc
|
---|
92 | // TypeMismatchExc
|
---|
93 | // DuplicateIdExc
|
---|
94 | // NotFoundExc
|
---|
95 | // MathExc
|
---|
96 | // SingMatxExc
|
---|
97 |
|
---|
98 | // generic IO Exception
|
---|
99 | class IOExc : public PException {
|
---|
100 | public:
|
---|
101 | explicit IOExc(const string& m, int id=0) : PException(m,id) {}
|
---|
102 | };
|
---|
103 |
|
---|
104 | // Bad type -> keep ?
|
---|
105 | class TypeMismatchExc : public PException {
|
---|
106 | public:
|
---|
107 | explicit TypeMismatchExc(const string& m, int id=0) : PException(m,id) {}
|
---|
108 | };
|
---|
109 |
|
---|
110 | class MathExc : public PException {
|
---|
111 | public:
|
---|
112 | explicit MathExc(const string& m, int id=0) : PException(m,id) {}
|
---|
113 | };
|
---|
114 |
|
---|
115 | class DuplicateIdExc : public PException {
|
---|
116 | public:
|
---|
117 | explicit DuplicateIdExc(const string& m, int id=0) : PException(m,id) {}
|
---|
118 | };
|
---|
119 |
|
---|
120 | class NotFoundExc : public PException {
|
---|
121 | public:
|
---|
122 | explicit NotFoundExc(const string& m, int id=0) : PException(m,id) {}
|
---|
123 | };
|
---|
124 |
|
---|
125 | class CaughtSignalExc : public PException {
|
---|
126 | public:
|
---|
127 | explicit CaughtSignalExc(const string& m, int id=0) : PException(m,id) {}
|
---|
128 | };
|
---|
129 |
|
---|
130 | // Bad file format
|
---|
131 | class FileFormatExc : public IOExc {
|
---|
132 | public:
|
---|
133 | explicit FileFormatExc(const string& m, int id=0) : IOExc(m,id) {}
|
---|
134 | };
|
---|
135 |
|
---|
136 | class SingMatrixExc : public MathExc {
|
---|
137 | public:
|
---|
138 | explicit SingMatrixExc(const string& m, int id=0) : MathExc(m,id) {}
|
---|
139 | };
|
---|
140 |
|
---|
141 |
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | #define ASSERT(_a_) if (!(_a_)) { \
|
---|
146 | cerr << "Assertion failed " #_a_ " file " __FILE__ " line " << __LINE__ \
|
---|
147 | << endl; \
|
---|
148 | throw(AssertionFailedError(#_a_)); }
|
---|
149 |
|
---|
150 | #define FAILNIL(_x_) \
|
---|
151 | {if (!(_x_)) throw(NullPtrError(#_x_))}
|
---|
152 |
|
---|
153 | void InitFailNewHandler();
|
---|
154 |
|
---|
155 |
|
---|
156 | // Compatibility with EROS-PEIDA exceptions
|
---|
157 |
|
---|
158 | #define EXC_ABORT_NEG(_x)
|
---|
159 | #define EXC_ABORT_ALL(_x)
|
---|
160 |
|
---|
161 | #define EXC_AWARE
|
---|
162 |
|
---|
163 | #define END_CONSTRUCTOR
|
---|
164 |
|
---|
165 | #define TRY try
|
---|
166 | #define CATCH(_var) catch(long _var)
|
---|
167 | #define CATCHALL catch(...)
|
---|
168 | #define ENDTRY
|
---|
169 |
|
---|
170 | #define THROW(_i) throw( _i);
|
---|
171 |
|
---|
172 | #define THROW_ THROW(0)
|
---|
173 |
|
---|
174 | #define THROW_SAME throw;
|
---|
175 |
|
---|
176 | #define RETURN(x) return(x)
|
---|
177 | #define RETURN_ return
|
---|
178 |
|
---|
179 | #define rangeCheckErr RangeCheckError("")
|
---|
180 | #define typeMismatchErr TypeMismatchExc("")
|
---|
181 | #define allocationErr AllocationError("")
|
---|
182 | #define parmErr ParmError("")
|
---|
183 | #define inconsistentErr ParmError("")
|
---|
184 | #define sizeMismatchErr SzMismatchError("")
|
---|
185 | #define fileErr IOExc("")
|
---|
186 | #define nullPtrErr NullPtrError("")
|
---|
187 | #define singMatxErr SingMatrixExc("")
|
---|
188 |
|
---|
189 | #define DBASSERT(_x_) ASSERT(_x_)
|
---|
190 |
|
---|
191 | #endif
|
---|