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