[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 |
|
---|
[773] | 10 | // Petit utilitaire pour accoler numero de ligne et nom de fichier aux messages
|
---|
| 11 | // d'exception
|
---|
| 12 | #define PExcLongMessage(a) BuildLongExceptionMessage(a,__FILE__,__LINE__)
|
---|
| 13 |
|
---|
[552] | 14 | namespace SOPHYA {
|
---|
[242] | 15 |
|
---|
[895] | 16 | //! Utility function for appending a file name and line number to a message
|
---|
[773] | 17 | string BuildLongExceptionMessage(const char * s, const char *file, int line);
|
---|
| 18 |
|
---|
[895] | 19 | //! Base exception class in Sophya.
|
---|
| 20 | /*! Ancestor for PError and PException
|
---|
| 21 | It has a message, and an id to give more
|
---|
| 22 | information on the exception.
|
---|
| 23 | */
|
---|
[256] | 24 | class PThrowable {
|
---|
| 25 | public:
|
---|
[895] | 26 | //! Constructor with the message and error-id (optional) specification
|
---|
[256] | 27 | explicit PThrowable(const string& m, int ident=0)
|
---|
| 28 | : msg(m), id(ident) {}
|
---|
| 29 | virtual ~PThrowable() { }
|
---|
[895] | 30 | //! Returns the associated message string
|
---|
[256] | 31 | virtual string const& Msg() const {return msg;}
|
---|
[895] | 32 | //! Returns the associated error-id
|
---|
| 33 | virtual int Id() const {return id; }
|
---|
[256] | 34 | private:
|
---|
| 35 | string msg;
|
---|
| 36 | int id;
|
---|
[242] | 37 | };
|
---|
[895] | 38 |
|
---|
| 39 | // PThrowable
|
---|
| 40 | // PError
|
---|
| 41 | // PException
|
---|
[256] | 42 |
|
---|
[895] | 43 | //! A PError is a serious logic error. Usually not caught...
|
---|
[242] | 44 | class PError : public PThrowable {
|
---|
[256] | 45 | public:
|
---|
| 46 | explicit PError(const string& m, int id=0) : PThrowable(m,id) {}
|
---|
[242] | 47 | };
|
---|
| 48 |
|
---|
[895] | 49 | //! A PException is not as serious... Can be caught.
|
---|
[242] | 50 | class PException : public PThrowable {
|
---|
[256] | 51 | public:
|
---|
| 52 | explicit PException(const string& m, int id=0) : PThrowable(m,id) {}
|
---|
[242] | 53 | };
|
---|
[256] | 54 |
|
---|
[895] | 55 | // ---- Errors ----
|
---|
| 56 | // PError
|
---|
| 57 | // AllocationError
|
---|
| 58 | // NullPtrError
|
---|
| 59 | // ForbiddenError
|
---|
| 60 | // AssertionFailedError
|
---|
| 61 |
|
---|
| 62 | //! Memory allocation failure
|
---|
[242] | 63 | class AllocationError : public PError {
|
---|
[256] | 64 | public:
|
---|
| 65 | explicit AllocationError(const string& m, int id=0) : PError(m,id) {}
|
---|
[242] | 66 | };
|
---|
| 67 |
|
---|
[895] | 68 | //! Null pointer error
|
---|
[242] | 69 | class NullPtrError : public PError {
|
---|
[256] | 70 | public:
|
---|
| 71 | explicit NullPtrError(const string& m, int id=0) : PError(m,id) {}
|
---|
[242] | 72 | };
|
---|
[256] | 73 |
|
---|
| 74 |
|
---|
[895] | 75 | //! Calling a forbidden method, trying a forbidden operation
|
---|
[242] | 76 | class ForbiddenError : public PError {
|
---|
[256] | 77 | public:
|
---|
| 78 | explicit ForbiddenError(const string& m, int id=0) : PError(m,id) {}
|
---|
[242] | 79 | };
|
---|
[749] | 80 |
|
---|
[256] | 81 |
|
---|
[895] | 82 | //! ASSERT macro failure. The message is the assertion...
|
---|
[242] | 83 | class AssertionFailedError : public PError {
|
---|
[256] | 84 | public:
|
---|
| 85 | explicit AssertionFailedError(const string& m, int id=0) : PError(m,id) {}
|
---|
[242] | 86 | };
|
---|
[256] | 87 |
|
---|
[242] | 88 | // Standard exceptions
|
---|
[256] | 89 | //
|
---|
| 90 | // PException
|
---|
| 91 | // IOExc
|
---|
| 92 | // FileFormatExc
|
---|
[895] | 93 | // SzMismatchError
|
---|
| 94 | // RangeCheckError
|
---|
| 95 | // ParmError
|
---|
[256] | 96 | // TypeMismatchExc
|
---|
[895] | 97 | // MathExc
|
---|
| 98 | // SingMatxExc
|
---|
[256] | 99 | // DuplicateIdExc
|
---|
| 100 | // NotFoundExc
|
---|
[895] | 101 | // CaughtSignalExc
|
---|
[256] | 102 |
|
---|
[895] | 103 | //! Generic IO Exception.
|
---|
[256] | 104 | class IOExc : public PException {
|
---|
| 105 | public:
|
---|
| 106 | explicit IOExc(const string& m, int id=0) : PException(m,id) {}
|
---|
[242] | 107 | };
|
---|
| 108 |
|
---|
[895] | 109 | //! Bad file format.
|
---|
| 110 | class FileFormatExc : public IOExc {
|
---|
| 111 | public:
|
---|
| 112 | explicit FileFormatExc(const string& m, int id=0) : IOExc(m,id) {}
|
---|
| 113 | };
|
---|
| 114 |
|
---|
| 115 | //! Size mismatch between objects.
|
---|
| 116 | class SzMismatchError : public PException {
|
---|
| 117 | public:
|
---|
| 118 | explicit SzMismatchError(const string& m, int id=0) : PException(m,id) {}
|
---|
| 119 | };
|
---|
| 120 |
|
---|
| 121 | //! Out of bounds for array, matrix, etc.
|
---|
| 122 | class RangeCheckError : public PException {
|
---|
| 123 | public:
|
---|
| 124 | explicit RangeCheckError(const string& m, int id=0) : PException(m,id) {}
|
---|
| 125 | };
|
---|
| 126 |
|
---|
| 127 | //! Invalid parameter to method/constructor...
|
---|
| 128 | class ParmError : public PException {
|
---|
| 129 | public:
|
---|
| 130 | explicit ParmError(const string& m, int id=0) : PException(m,id) {}
|
---|
| 131 | };
|
---|
| 132 |
|
---|
| 133 | //! Calling a non available / not implemented method
|
---|
| 134 | class NotAvailableOperation : public PException {
|
---|
| 135 | public:
|
---|
| 136 | explicit NotAvailableOperation(const string& m, int id=0) : PException(m,id) {}
|
---|
| 137 | };
|
---|
| 138 |
|
---|
| 139 | //! Bad data type -> keep ?
|
---|
[256] | 140 | class TypeMismatchExc : public PException {
|
---|
| 141 | public:
|
---|
| 142 | explicit TypeMismatchExc(const string& m, int id=0) : PException(m,id) {}
|
---|
[242] | 143 | };
|
---|
[895] | 144 |
|
---|
| 145 | //! Math operation exception
|
---|
[256] | 146 | class MathExc : public PException {
|
---|
| 147 | public:
|
---|
| 148 | explicit MathExc(const string& m, int id=0) : PException(m,id) {}
|
---|
[242] | 149 | };
|
---|
[256] | 150 |
|
---|
[895] | 151 | //! Singular matrix
|
---|
| 152 | class SingMatrixExc : public MathExc {
|
---|
| 153 | public:
|
---|
| 154 | explicit SingMatrixExc(const string& m, int id=0) : MathExc(m,id) {}
|
---|
| 155 | };
|
---|
| 156 |
|
---|
| 157 | //! Duplicate identifier during registration
|
---|
[242] | 158 | class DuplicateIdExc : public PException {
|
---|
[256] | 159 | public:
|
---|
| 160 | explicit DuplicateIdExc(const string& m, int id=0) : PException(m,id) {}
|
---|
[242] | 161 | };
|
---|
[256] | 162 |
|
---|
[895] | 163 | //! Not found identifier
|
---|
[242] | 164 | class NotFoundExc : public PException {
|
---|
[256] | 165 | public:
|
---|
| 166 | explicit NotFoundExc(const string& m, int id=0) : PException(m,id) {}
|
---|
[242] | 167 | };
|
---|
[256] | 168 |
|
---|
[895] | 169 | //! Generated exception when processing a signal
|
---|
[242] | 170 | class CaughtSignalExc : public PException {
|
---|
[256] | 171 | public:
|
---|
| 172 | explicit CaughtSignalExc(const string& m, int id=0) : PException(m,id) {}
|
---|
[242] | 173 | };
|
---|
[895] | 174 |
|
---|
| 175 | } // namespace SOPHYA
|
---|
[256] | 176 |
|
---|
[242] | 177 |
|
---|
| 178 | #define ASSERT(_a_) if (!(_a_)) { \
|
---|
| 179 | cerr << "Assertion failed " #_a_ " file " __FILE__ " line " << __LINE__ \
|
---|
| 180 | << endl; \
|
---|
| 181 | throw(AssertionFailedError(#_a_)); }
|
---|
| 182 |
|
---|
| 183 | #define FAILNIL(_x_) \
|
---|
| 184 | {if (!(_x_)) throw(NullPtrError(#_x_))}
|
---|
| 185 |
|
---|
| 186 | void InitFailNewHandler();
|
---|
| 187 |
|
---|
| 188 |
|
---|
| 189 | // Compatibility with EROS-PEIDA exceptions
|
---|
| 190 |
|
---|
| 191 | #define EXC_ABORT_NEG(_x)
|
---|
| 192 | #define EXC_ABORT_ALL(_x)
|
---|
| 193 |
|
---|
| 194 | #define EXC_AWARE
|
---|
| 195 |
|
---|
| 196 | #define END_CONSTRUCTOR
|
---|
| 197 |
|
---|
| 198 | #define TRY try
|
---|
| 199 | #define CATCH(_var) catch(long _var)
|
---|
| 200 | #define CATCHALL catch(...)
|
---|
| 201 | #define ENDTRY
|
---|
| 202 |
|
---|
| 203 | #define THROW(_i) throw( _i);
|
---|
| 204 |
|
---|
| 205 | #define THROW_ THROW(0)
|
---|
| 206 |
|
---|
| 207 | #define THROW_SAME throw;
|
---|
| 208 |
|
---|
| 209 | #define RETURN(x) return(x)
|
---|
| 210 | #define RETURN_ return
|
---|
| 211 |
|
---|
| 212 | #define rangeCheckErr RangeCheckError("")
|
---|
| 213 | #define typeMismatchErr TypeMismatchExc("")
|
---|
| 214 | #define allocationErr AllocationError("")
|
---|
| 215 | #define parmErr ParmError("")
|
---|
| 216 | #define inconsistentErr ParmError("")
|
---|
| 217 | #define sizeMismatchErr SzMismatchError("")
|
---|
[256] | 218 | #define fileErr IOExc("")
|
---|
[242] | 219 | #define nullPtrErr NullPtrError("")
|
---|
| 220 | #define singMatxErr SingMatrixExc("")
|
---|
| 221 |
|
---|
| 222 | #define DBASSERT(_x_) ASSERT(_x_)
|
---|
| 223 |
|
---|
| 224 | #endif
|
---|