1 | // |
---|
2 | // Exception.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/include/Poco/Exception.h#4 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Core |
---|
8 | // Module: Exception |
---|
9 | // |
---|
10 | // Definition of various Poco exception classes. |
---|
11 | // |
---|
12 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
13 | // and Contributors. |
---|
14 | // |
---|
15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Foundation_Exception_INCLUDED |
---|
40 | #define Foundation_Exception_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Foundation.h" |
---|
44 | #include <stdexcept> |
---|
45 | |
---|
46 | |
---|
47 | namespace Poco { |
---|
48 | |
---|
49 | |
---|
50 | class Foundation_API Exception: public std::exception |
---|
51 | /// This is the base class for all exceptions defined |
---|
52 | /// in the Poco class library. |
---|
53 | { |
---|
54 | public: |
---|
55 | Exception(const std::string& msg, int code = 0); |
---|
56 | /// Creates an exception. |
---|
57 | |
---|
58 | Exception(const std::string& msg, const std::string& arg, int code = 0); |
---|
59 | /// Creates an exception. |
---|
60 | |
---|
61 | Exception(const std::string& msg, const Exception& nested, int code = 0); |
---|
62 | /// Creates an exception and stores a clone |
---|
63 | /// of the nested exception. |
---|
64 | |
---|
65 | Exception(const Exception& exc); |
---|
66 | /// Copy constructor. |
---|
67 | |
---|
68 | ~Exception() throw(); |
---|
69 | /// Destroys the exception and deletes the nested exception. |
---|
70 | |
---|
71 | Exception& operator = (const Exception& exc); |
---|
72 | /// Assignment operator. |
---|
73 | |
---|
74 | virtual const char* name() const throw(); |
---|
75 | /// Returns a static string describing the exception. |
---|
76 | |
---|
77 | virtual const char* className() const throw(); |
---|
78 | /// Returns the name of the exception class. |
---|
79 | |
---|
80 | virtual const char* what() const throw(); |
---|
81 | /// Returns a static string describing the exception. |
---|
82 | /// |
---|
83 | /// Same as name(), but for compatibility with std::exception. |
---|
84 | |
---|
85 | const Exception* nested() const; |
---|
86 | /// Returns a pointer to the nested exception, or |
---|
87 | /// null if no nested exception exists. |
---|
88 | |
---|
89 | const std::string& message() const; |
---|
90 | /// Returns the message text. |
---|
91 | |
---|
92 | int code() const; |
---|
93 | /// Returns the exception code if defined. |
---|
94 | |
---|
95 | std::string displayText() const; |
---|
96 | /// Returns a string consisting of the |
---|
97 | /// message name and the message text. |
---|
98 | |
---|
99 | virtual Exception* clone() const; |
---|
100 | /// Creates an exact copy of the exception. |
---|
101 | /// |
---|
102 | /// The copy can later be thrown again by |
---|
103 | /// invoking rethrow() on it. |
---|
104 | |
---|
105 | virtual void rethrow() const; |
---|
106 | /// (Re)Throws the exception. |
---|
107 | /// |
---|
108 | /// This is useful for temporarily storing a |
---|
109 | /// copy of an exception (see clone()), then |
---|
110 | /// throwing it again. |
---|
111 | |
---|
112 | protected: |
---|
113 | Exception(int code = 0); |
---|
114 | /// Standard constructor. |
---|
115 | |
---|
116 | void message(const std::string& msg); |
---|
117 | /// Sets the message for the exception. |
---|
118 | |
---|
119 | void extendedMessage(const std::string& arg); |
---|
120 | /// Sets the extended message for the exception. |
---|
121 | |
---|
122 | private: |
---|
123 | std::string _msg; |
---|
124 | Exception* _pNested; |
---|
125 | int _code; |
---|
126 | }; |
---|
127 | |
---|
128 | |
---|
129 | // |
---|
130 | // inlines |
---|
131 | // |
---|
132 | inline const Exception* Exception::nested() const |
---|
133 | { |
---|
134 | return _pNested; |
---|
135 | } |
---|
136 | |
---|
137 | |
---|
138 | inline const std::string& Exception::message() const |
---|
139 | { |
---|
140 | return _msg; |
---|
141 | } |
---|
142 | |
---|
143 | |
---|
144 | inline void Exception::message(const std::string& msg) |
---|
145 | { |
---|
146 | _msg = msg; |
---|
147 | } |
---|
148 | |
---|
149 | |
---|
150 | inline int Exception::code() const |
---|
151 | { |
---|
152 | return _code; |
---|
153 | } |
---|
154 | |
---|
155 | |
---|
156 | // |
---|
157 | // Macros for quickly declaring and implementing exception classes. |
---|
158 | // Unfortunately, we cannot use a template here because character |
---|
159 | // pointers (which we need for specifying the exception name) |
---|
160 | // are not allowed as template arguments. |
---|
161 | // |
---|
162 | #define POCO_DECLARE_EXCEPTION(API, CLS, BASE) \ |
---|
163 | class API CLS: public BASE \ |
---|
164 | { \ |
---|
165 | public: \ |
---|
166 | CLS(int code = 0); \ |
---|
167 | CLS(const std::string& msg, int code = 0); \ |
---|
168 | CLS(const std::string& msg, const std::string& arg, int code = 0); \ |
---|
169 | CLS(const std::string& msg, const Poco::Exception& exc, int code = 0); \ |
---|
170 | CLS(const CLS& exc); \ |
---|
171 | ~CLS() throw(); \ |
---|
172 | CLS& operator = (const CLS& exc); \ |
---|
173 | const char* name() const throw(); \ |
---|
174 | const char* className() const throw(); \ |
---|
175 | Poco::Exception* clone() const; \ |
---|
176 | void rethrow() const; \ |
---|
177 | }; |
---|
178 | |
---|
179 | |
---|
180 | #define POCO_IMPLEMENT_EXCEPTION(CLS, BASE, NAME) \ |
---|
181 | CLS::CLS(int code): BASE(code) \ |
---|
182 | { \ |
---|
183 | } \ |
---|
184 | CLS::CLS(const std::string& msg, int code): BASE(msg, code) \ |
---|
185 | { \ |
---|
186 | } \ |
---|
187 | CLS::CLS(const std::string& msg, const std::string& arg, int code): BASE(msg, arg, code) \ |
---|
188 | { \ |
---|
189 | } \ |
---|
190 | CLS::CLS(const std::string& msg, const Poco::Exception& exc, int code): BASE(msg, exc, code) \ |
---|
191 | { \ |
---|
192 | } \ |
---|
193 | CLS::CLS(const CLS& exc): BASE(exc) \ |
---|
194 | { \ |
---|
195 | } \ |
---|
196 | CLS::~CLS() throw() \ |
---|
197 | { \ |
---|
198 | } \ |
---|
199 | CLS& CLS::operator = (const CLS& exc) \ |
---|
200 | { \ |
---|
201 | BASE::operator = (exc); \ |
---|
202 | return *this; \ |
---|
203 | } \ |
---|
204 | const char* CLS::name() const throw() \ |
---|
205 | { \ |
---|
206 | return NAME; \ |
---|
207 | } \ |
---|
208 | const char* CLS::className() const throw() \ |
---|
209 | { \ |
---|
210 | return typeid(*this).name(); \ |
---|
211 | } \ |
---|
212 | Poco::Exception* CLS::clone() const \ |
---|
213 | { \ |
---|
214 | return new CLS(*this); \ |
---|
215 | } \ |
---|
216 | void CLS::rethrow() const \ |
---|
217 | { \ |
---|
218 | throw *this; \ |
---|
219 | } |
---|
220 | |
---|
221 | |
---|
222 | // |
---|
223 | // Standard exception classes |
---|
224 | // |
---|
225 | POCO_DECLARE_EXCEPTION(Foundation_API, LogicException, Exception) |
---|
226 | POCO_DECLARE_EXCEPTION(Foundation_API, AssertionViolationException, LogicException) |
---|
227 | POCO_DECLARE_EXCEPTION(Foundation_API, NullPointerException, LogicException) |
---|
228 | POCO_DECLARE_EXCEPTION(Foundation_API, BugcheckException, LogicException) |
---|
229 | POCO_DECLARE_EXCEPTION(Foundation_API, InvalidArgumentException, LogicException) |
---|
230 | POCO_DECLARE_EXCEPTION(Foundation_API, NotImplementedException, LogicException) |
---|
231 | POCO_DECLARE_EXCEPTION(Foundation_API, RangeException, LogicException) |
---|
232 | POCO_DECLARE_EXCEPTION(Foundation_API, IllegalStateException, LogicException) |
---|
233 | POCO_DECLARE_EXCEPTION(Foundation_API, InvalidAccessException, LogicException) |
---|
234 | POCO_DECLARE_EXCEPTION(Foundation_API, SignalException, LogicException) |
---|
235 | POCO_DECLARE_EXCEPTION(Foundation_API, UnhandledException, LogicException) |
---|
236 | |
---|
237 | POCO_DECLARE_EXCEPTION(Foundation_API, RuntimeException, Exception) |
---|
238 | POCO_DECLARE_EXCEPTION(Foundation_API, NotFoundException, RuntimeException) |
---|
239 | POCO_DECLARE_EXCEPTION(Foundation_API, ExistsException, RuntimeException) |
---|
240 | POCO_DECLARE_EXCEPTION(Foundation_API, TimeoutException, RuntimeException) |
---|
241 | POCO_DECLARE_EXCEPTION(Foundation_API, SystemException, RuntimeException) |
---|
242 | POCO_DECLARE_EXCEPTION(Foundation_API, RegularExpressionException, RuntimeException) |
---|
243 | POCO_DECLARE_EXCEPTION(Foundation_API, LibraryLoadException, RuntimeException) |
---|
244 | POCO_DECLARE_EXCEPTION(Foundation_API, LibraryAlreadyLoadedException, RuntimeException) |
---|
245 | POCO_DECLARE_EXCEPTION(Foundation_API, NoThreadAvailableException, RuntimeException) |
---|
246 | POCO_DECLARE_EXCEPTION(Foundation_API, PropertyNotSupportedException, RuntimeException) |
---|
247 | POCO_DECLARE_EXCEPTION(Foundation_API, PoolOverflowException, RuntimeException) |
---|
248 | POCO_DECLARE_EXCEPTION(Foundation_API, NoPermissionException, RuntimeException) |
---|
249 | POCO_DECLARE_EXCEPTION(Foundation_API, OutOfMemoryException, RuntimeException) |
---|
250 | POCO_DECLARE_EXCEPTION(Foundation_API, DataException, RuntimeException) |
---|
251 | |
---|
252 | POCO_DECLARE_EXCEPTION(Foundation_API, DataFormatException, DataException) |
---|
253 | POCO_DECLARE_EXCEPTION(Foundation_API, SyntaxException, DataException) |
---|
254 | POCO_DECLARE_EXCEPTION(Foundation_API, CircularReferenceException, DataException) |
---|
255 | POCO_DECLARE_EXCEPTION(Foundation_API, PathSyntaxException, SyntaxException) |
---|
256 | POCO_DECLARE_EXCEPTION(Foundation_API, IOException, RuntimeException) |
---|
257 | POCO_DECLARE_EXCEPTION(Foundation_API, ProtocolException, IOException) |
---|
258 | POCO_DECLARE_EXCEPTION(Foundation_API, FileException, IOException) |
---|
259 | POCO_DECLARE_EXCEPTION(Foundation_API, FileExistsException, FileException) |
---|
260 | POCO_DECLARE_EXCEPTION(Foundation_API, FileNotFoundException, FileException) |
---|
261 | POCO_DECLARE_EXCEPTION(Foundation_API, PathNotFoundException, FileException) |
---|
262 | POCO_DECLARE_EXCEPTION(Foundation_API, FileReadOnlyException, FileException) |
---|
263 | POCO_DECLARE_EXCEPTION(Foundation_API, FileAccessDeniedException, FileException) |
---|
264 | POCO_DECLARE_EXCEPTION(Foundation_API, CreateFileException, FileException) |
---|
265 | POCO_DECLARE_EXCEPTION(Foundation_API, OpenFileException, FileException) |
---|
266 | POCO_DECLARE_EXCEPTION(Foundation_API, WriteFileException, FileException) |
---|
267 | POCO_DECLARE_EXCEPTION(Foundation_API, ReadFileException, FileException) |
---|
268 | POCO_DECLARE_EXCEPTION(Foundation_API, UnknownURISchemeException, RuntimeException) |
---|
269 | |
---|
270 | POCO_DECLARE_EXCEPTION(Foundation_API, ApplicationException, Exception) |
---|
271 | POCO_DECLARE_EXCEPTION(Foundation_API, BadCastException, RuntimeException) |
---|
272 | |
---|
273 | |
---|
274 | } // namespace Poco |
---|
275 | |
---|
276 | |
---|
277 | #endif // Foundation_Exception_INCLUDED |
---|