source: Sophya/trunk/SophyaLib/BaseTools/pexceptions.h@ 3670

Last change on this file since 3670 was 3585, checked in by ansari, 17 years ago

Ajout heritage exceptions SOPHYA de std::exception (+method what()) - nettoyage/suppression TRY/CATCH... , Reza 05/03/2009

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