1 | // |
2 | // Exception.h |
3 | // |
4 | // Library: Foundation |
5 | // Package: Core |
6 | // Module: Exception |
7 | // |
8 | // Definition of various Poco exception classes. |
9 | // |
10 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
11 | // and Contributors. |
12 | // |
13 | // SPDX-License-Identifier: BSL-1.0 |
14 | // |
15 | |
16 | |
17 | #ifndef Foundation_Exception_INCLUDED |
18 | #define Foundation_Exception_INCLUDED |
19 | |
20 | |
21 | #include "Poco/Foundation.h" |
22 | #include <stdexcept> |
23 | |
24 | |
25 | namespace Poco { |
26 | |
27 | |
28 | class Foundation_API Exception: public std::exception |
29 | /// This is the base class for all exceptions defined |
30 | /// in the Poco class library. |
31 | { |
32 | public: |
33 | Exception(const std::string& msg, int code = 0); |
34 | /// Creates an exception. |
35 | |
36 | Exception(const std::string& msg, const std::string& arg, int code = 0); |
37 | /// Creates an exception. |
38 | |
39 | Exception(const std::string& msg, const Exception& nested, int code = 0); |
40 | /// Creates an exception and stores a clone |
41 | /// of the nested exception. |
42 | |
43 | Exception(const Exception& exc); |
44 | /// Copy constructor. |
45 | |
46 | ~Exception() throw(); |
47 | /// Destroys the exception and deletes the nested exception. |
48 | |
49 | Exception& operator = (const Exception& exc); |
50 | /// Assignment operator. |
51 | |
52 | virtual const char* name() const throw(); |
53 | /// Returns a static string describing the exception. |
54 | |
55 | virtual const char* className() const throw(); |
56 | /// Returns the name of the exception class. |
57 | |
58 | virtual const char* what() const throw(); |
59 | /// Returns a static string describing the exception. |
60 | /// |
61 | /// Same as name(), but for compatibility with std::exception. |
62 | |
63 | const Exception* nested() const; |
64 | /// Returns a pointer to the nested exception, or |
65 | /// null if no nested exception exists. |
66 | |
67 | const std::string& message() const; |
68 | /// Returns the message text. |
69 | |
70 | int code() const; |
71 | /// Returns the exception code if defined. |
72 | |
73 | std::string displayText() const; |
74 | /// Returns a string consisting of the |
75 | /// message name and the message text. |
76 | |
77 | virtual Exception* clone() const; |
78 | /// Creates an exact copy of the exception. |
79 | /// |
80 | /// The copy can later be thrown again by |
81 | /// invoking rethrow() on it. |
82 | |
83 | virtual void rethrow() const; |
84 | /// (Re)Throws the exception. |
85 | /// |
86 | /// This is useful for temporarily storing a |
87 | /// copy of an exception (see clone()), then |
88 | /// throwing it again. |
89 | |
90 | protected: |
91 | Exception(int code = 0); |
92 | /// Standard constructor. |
93 | |
94 | void message(const std::string& msg); |
95 | /// Sets the message for the exception. |
96 | |
97 | void extendedMessage(const std::string& arg); |
98 | /// Sets the extended message for the exception. |
99 | |
100 | void addBacktrace(); |
101 | /// Appends backtrace (if available) to the |
102 | /// message. |
103 | |
104 | private: |
105 | std::string& msg() const |
106 | { |
107 | if (_msg.find(name()) == std::string::npos) |
108 | { |
109 | std::string s(name()); |
110 | s.append(": " ); |
111 | _msg.insert(0, s); |
112 | } |
113 | return _msg; |
114 | } |
115 | |
116 | mutable std::string _msg; |
117 | Exception* _pNested; |
118 | int _code; |
119 | }; |
120 | |
121 | |
122 | // |
123 | // inlines |
124 | // |
125 | inline const Exception* Exception::nested() const |
126 | { |
127 | return _pNested; |
128 | } |
129 | |
130 | |
131 | inline const std::string& Exception::message() const |
132 | { |
133 | return _msg; |
134 | } |
135 | |
136 | |
137 | inline void Exception::message(const std::string& msg) |
138 | { |
139 | _msg = msg; |
140 | } |
141 | |
142 | |
143 | inline int Exception::code() const |
144 | { |
145 | return _code; |
146 | } |
147 | |
148 | |
149 | // |
150 | // Macros for quickly declaring and implementing exception classes. |
151 | // Unfortunately, we cannot use a template here because character |
152 | // pointers (which we need for specifying the exception name) |
153 | // are not allowed as template arguments. |
154 | // |
155 | #define POCO_DECLARE_EXCEPTION_CODE(API, CLS, BASE, CODE) \ |
156 | class API CLS: public BASE \ |
157 | { \ |
158 | public: \ |
159 | CLS(int code = CODE); \ |
160 | CLS(const std::string& msg, int code = CODE); \ |
161 | CLS(const std::string& msg, const std::string& arg, int code = CODE); \ |
162 | CLS(const std::string& msg, const Poco::Exception& exc, int code = CODE); \ |
163 | CLS(const CLS& exc); \ |
164 | ~CLS() throw(); \ |
165 | CLS& operator = (const CLS& exc); \ |
166 | const char* name() const throw(); \ |
167 | const char* className() const throw(); \ |
168 | Poco::Exception* clone() const; \ |
169 | void rethrow() const; \ |
170 | }; |
171 | |
172 | #define POCO_DECLARE_EXCEPTION(API, CLS, BASE) \ |
173 | POCO_DECLARE_EXCEPTION_CODE(API, CLS, BASE, 0) |
174 | |
175 | #define POCO_IMPLEMENT_EXCEPTION(CLS, BASE, NAME) \ |
176 | CLS::CLS(int otherCode): BASE(otherCode) \ |
177 | { \ |
178 | } \ |
179 | CLS::CLS(const std::string& msg, int otherCode): BASE(msg, otherCode) \ |
180 | { \ |
181 | } \ |
182 | CLS::CLS(const std::string& msg, const std::string& arg, int otherCode): BASE(msg, arg, otherCode) \ |
183 | { \ |
184 | } \ |
185 | CLS::CLS(const std::string& msg, const Poco::Exception& exc, int otherCode): BASE(msg, exc, otherCode) \ |
186 | { \ |
187 | } \ |
188 | CLS::CLS(const CLS& exc): BASE(exc) \ |
189 | { \ |
190 | } \ |
191 | CLS::~CLS() throw() \ |
192 | { \ |
193 | } \ |
194 | CLS& CLS::operator = (const CLS& exc) \ |
195 | { \ |
196 | BASE::operator = (exc); \ |
197 | return *this; \ |
198 | } \ |
199 | const char* CLS::name() const throw() \ |
200 | { \ |
201 | return NAME; \ |
202 | } \ |
203 | const char* CLS::className() const throw() \ |
204 | { \ |
205 | return typeid(*this).name(); \ |
206 | } \ |
207 | Poco::Exception* CLS::clone() const \ |
208 | { \ |
209 | return new CLS(*this); \ |
210 | } \ |
211 | void CLS::rethrow() const \ |
212 | { \ |
213 | throw *this; \ |
214 | } |
215 | |
216 | |
217 | // |
218 | // Standard exception classes |
219 | // |
220 | POCO_DECLARE_EXCEPTION(Foundation_API, LogicException, Exception) |
221 | POCO_DECLARE_EXCEPTION(Foundation_API, AssertionViolationException, LogicException) |
222 | POCO_DECLARE_EXCEPTION(Foundation_API, NullPointerException, LogicException) |
223 | POCO_DECLARE_EXCEPTION(Foundation_API, NullValueException, LogicException) |
224 | POCO_DECLARE_EXCEPTION(Foundation_API, BugcheckException, LogicException) |
225 | POCO_DECLARE_EXCEPTION(Foundation_API, InvalidArgumentException, LogicException) |
226 | POCO_DECLARE_EXCEPTION(Foundation_API, NotImplementedException, LogicException) |
227 | POCO_DECLARE_EXCEPTION(Foundation_API, RangeException, LogicException) |
228 | POCO_DECLARE_EXCEPTION(Foundation_API, IllegalStateException, LogicException) |
229 | POCO_DECLARE_EXCEPTION(Foundation_API, InvalidAccessException, LogicException) |
230 | POCO_DECLARE_EXCEPTION(Foundation_API, SignalException, LogicException) |
231 | POCO_DECLARE_EXCEPTION(Foundation_API, UnhandledException, LogicException) |
232 | |
233 | POCO_DECLARE_EXCEPTION(Foundation_API, RuntimeException, Exception) |
234 | POCO_DECLARE_EXCEPTION(Foundation_API, NotFoundException, RuntimeException) |
235 | POCO_DECLARE_EXCEPTION(Foundation_API, ExistsException, RuntimeException) |
236 | POCO_DECLARE_EXCEPTION(Foundation_API, TimeoutException, RuntimeException) |
237 | POCO_DECLARE_EXCEPTION(Foundation_API, SystemException, RuntimeException) |
238 | POCO_DECLARE_EXCEPTION(Foundation_API, RegularExpressionException, RuntimeException) |
239 | POCO_DECLARE_EXCEPTION(Foundation_API, LibraryLoadException, RuntimeException) |
240 | POCO_DECLARE_EXCEPTION(Foundation_API, LibraryAlreadyLoadedException, RuntimeException) |
241 | POCO_DECLARE_EXCEPTION(Foundation_API, NoThreadAvailableException, RuntimeException) |
242 | POCO_DECLARE_EXCEPTION(Foundation_API, PropertyNotSupportedException, RuntimeException) |
243 | POCO_DECLARE_EXCEPTION(Foundation_API, PoolOverflowException, RuntimeException) |
244 | POCO_DECLARE_EXCEPTION(Foundation_API, NoPermissionException, RuntimeException) |
245 | POCO_DECLARE_EXCEPTION(Foundation_API, OutOfMemoryException, RuntimeException) |
246 | POCO_DECLARE_EXCEPTION(Foundation_API, DataException, RuntimeException) |
247 | POCO_DECLARE_EXCEPTION(Foundation_API, InterruptedException, RuntimeException) |
248 | POCO_DECLARE_EXCEPTION(Foundation_API, IndexOutOfBoundsException, RuntimeException) |
249 | POCO_DECLARE_EXCEPTION(Foundation_API, UnsupportedOperationException, RuntimeException) |
250 | POCO_DECLARE_EXCEPTION(Foundation_API, EmptyStackException, RuntimeException) |
251 | POCO_DECLARE_EXCEPTION(Foundation_API, StackOverflowException, RuntimeException) |
252 | POCO_DECLARE_EXCEPTION(Foundation_API, ArithmeticException, RuntimeException) |
253 | |
254 | POCO_DECLARE_EXCEPTION(Foundation_API, DataFormatException, DataException) |
255 | POCO_DECLARE_EXCEPTION(Foundation_API, SyntaxException, DataException) |
256 | POCO_DECLARE_EXCEPTION(Foundation_API, CircularReferenceException, DataException) |
257 | POCO_DECLARE_EXCEPTION(Foundation_API, PathSyntaxException, SyntaxException) |
258 | POCO_DECLARE_EXCEPTION(Foundation_API, IOException, RuntimeException) |
259 | POCO_DECLARE_EXCEPTION(Foundation_API, ProtocolException, IOException) |
260 | POCO_DECLARE_EXCEPTION(Foundation_API, FileException, IOException) |
261 | POCO_DECLARE_EXCEPTION(Foundation_API, FileExistsException, FileException) |
262 | POCO_DECLARE_EXCEPTION(Foundation_API, FileNotFoundException, FileException) |
263 | POCO_DECLARE_EXCEPTION(Foundation_API, PathNotFoundException, FileException) |
264 | POCO_DECLARE_EXCEPTION(Foundation_API, FileReadOnlyException, FileException) |
265 | POCO_DECLARE_EXCEPTION(Foundation_API, FileAccessDeniedException, FileException) |
266 | POCO_DECLARE_EXCEPTION(Foundation_API, CreateFileException, FileException) |
267 | POCO_DECLARE_EXCEPTION(Foundation_API, OpenFileException, FileException) |
268 | POCO_DECLARE_EXCEPTION(Foundation_API, WriteFileException, FileException) |
269 | POCO_DECLARE_EXCEPTION(Foundation_API, ReadFileException, FileException) |
270 | POCO_DECLARE_EXCEPTION(Foundation_API, DirectoryNotEmptyException, FileException) |
271 | POCO_DECLARE_EXCEPTION(Foundation_API, UnknownURISchemeException, RuntimeException) |
272 | POCO_DECLARE_EXCEPTION(Foundation_API, TooManyURIRedirectsException, RuntimeException) |
273 | POCO_DECLARE_EXCEPTION(Foundation_API, URISyntaxException, SyntaxException) |
274 | |
275 | POCO_DECLARE_EXCEPTION(Foundation_API, ApplicationException, Exception) |
276 | POCO_DECLARE_EXCEPTION(Foundation_API, BadCastException, RuntimeException) |
277 | |
278 | |
279 | } // namespace Poco |
280 | |
281 | |
282 | #endif // Foundation_Exception_INCLUDED |
283 | |