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