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
25namespace Poco {
26
27
28class Foundation_API Exception: public std::exception
29 /// This is the base class for all exceptions defined
30 /// in the Poco class library.
31{
32public:
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
90protected:
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
100private:
101 std::string _msg;
102 Exception* _pNested;
103 int _code;
104};
105
106
107//
108// inlines
109//
110inline const Exception* Exception::nested() const
111{
112 return _pNested;
113}
114
115
116inline const std::string& Exception::message() const
117{
118 return _msg;
119}
120
121
122inline void Exception::message(const std::string& msg)
123{
124 _msg = msg;
125}
126
127
128inline int Exception::code() const
129{
130 return _code;
131}
132
133
134//
135// Macros for quickly declaring and implementing exception classes.
136// Unfortunately, we cannot use a template here because character
137// pointers (which we need for specifying the exception name)
138// are not allowed as template arguments.
139//
140#define POCO_DECLARE_EXCEPTION_CODE(API, CLS, BASE, CODE) \
141 class API CLS: public BASE \
142 { \
143 public: \
144 CLS(int code = CODE); \
145 CLS(const std::string& msg, int code = CODE); \
146 CLS(const std::string& msg, const std::string& arg, int code = CODE); \
147 CLS(const std::string& msg, const Poco::Exception& exc, int code = CODE); \
148 CLS(const CLS& exc); \
149 ~CLS() throw(); \
150 CLS& operator = (const CLS& exc); \
151 const char* name() const throw(); \
152 const char* className() const throw(); \
153 Poco::Exception* clone() const; \
154 void rethrow() const; \
155 };
156
157#define POCO_DECLARE_EXCEPTION(API, CLS, BASE) \
158 POCO_DECLARE_EXCEPTION_CODE(API, CLS, BASE, 0)
159
160#define POCO_IMPLEMENT_EXCEPTION(CLS, BASE, NAME) \
161 CLS::CLS(int otherCode): BASE(otherCode) \
162 { \
163 } \
164 CLS::CLS(const std::string& msg, int otherCode): BASE(msg, otherCode) \
165 { \
166 } \
167 CLS::CLS(const std::string& msg, const std::string& arg, int otherCode): BASE(msg, arg, otherCode) \
168 { \
169 } \
170 CLS::CLS(const std::string& msg, const Poco::Exception& exc, int otherCode): BASE(msg, exc, otherCode) \
171 { \
172 } \
173 CLS::CLS(const CLS& exc): BASE(exc) \
174 { \
175 } \
176 CLS::~CLS() throw() \
177 { \
178 } \
179 CLS& CLS::operator = (const CLS& exc) \
180 { \
181 BASE::operator = (exc); \
182 return *this; \
183 } \
184 const char* CLS::name() const throw() \
185 { \
186 return NAME; \
187 } \
188 const char* CLS::className() const throw() \
189 { \
190 return typeid(*this).name(); \
191 } \
192 Poco::Exception* CLS::clone() const \
193 { \
194 return new CLS(*this); \
195 } \
196 void CLS::rethrow() const \
197 { \
198 throw *this; \
199 }
200
201
202//
203// Standard exception classes
204//
205POCO_DECLARE_EXCEPTION(Foundation_API, LogicException, Exception)
206POCO_DECLARE_EXCEPTION(Foundation_API, AssertionViolationException, LogicException)
207POCO_DECLARE_EXCEPTION(Foundation_API, NullPointerException, LogicException)
208POCO_DECLARE_EXCEPTION(Foundation_API, NullValueException, LogicException)
209POCO_DECLARE_EXCEPTION(Foundation_API, BugcheckException, LogicException)
210POCO_DECLARE_EXCEPTION(Foundation_API, InvalidArgumentException, LogicException)
211POCO_DECLARE_EXCEPTION(Foundation_API, NotImplementedException, LogicException)
212POCO_DECLARE_EXCEPTION(Foundation_API, RangeException, LogicException)
213POCO_DECLARE_EXCEPTION(Foundation_API, IllegalStateException, LogicException)
214POCO_DECLARE_EXCEPTION(Foundation_API, InvalidAccessException, LogicException)
215POCO_DECLARE_EXCEPTION(Foundation_API, SignalException, LogicException)
216POCO_DECLARE_EXCEPTION(Foundation_API, UnhandledException, LogicException)
217
218POCO_DECLARE_EXCEPTION(Foundation_API, RuntimeException, Exception)
219POCO_DECLARE_EXCEPTION(Foundation_API, NotFoundException, RuntimeException)
220POCO_DECLARE_EXCEPTION(Foundation_API, ExistsException, RuntimeException)
221POCO_DECLARE_EXCEPTION(Foundation_API, TimeoutException, RuntimeException)
222POCO_DECLARE_EXCEPTION(Foundation_API, SystemException, RuntimeException)
223POCO_DECLARE_EXCEPTION(Foundation_API, RegularExpressionException, RuntimeException)
224POCO_DECLARE_EXCEPTION(Foundation_API, LibraryLoadException, RuntimeException)
225POCO_DECLARE_EXCEPTION(Foundation_API, LibraryAlreadyLoadedException, RuntimeException)
226POCO_DECLARE_EXCEPTION(Foundation_API, NoThreadAvailableException, RuntimeException)
227POCO_DECLARE_EXCEPTION(Foundation_API, PropertyNotSupportedException, RuntimeException)
228POCO_DECLARE_EXCEPTION(Foundation_API, PoolOverflowException, RuntimeException)
229POCO_DECLARE_EXCEPTION(Foundation_API, NoPermissionException, RuntimeException)
230POCO_DECLARE_EXCEPTION(Foundation_API, OutOfMemoryException, RuntimeException)
231POCO_DECLARE_EXCEPTION(Foundation_API, DataException, RuntimeException)
232POCO_DECLARE_EXCEPTION(Foundation_API, InterruptedException, RuntimeException)
233POCO_DECLARE_EXCEPTION(Foundation_API, IndexOutOfBoundsException, RuntimeException)
234POCO_DECLARE_EXCEPTION(Foundation_API, UnsupportedOperationException, RuntimeException)
235POCO_DECLARE_EXCEPTION(Foundation_API, EmptyStackException, RuntimeException)
236POCO_DECLARE_EXCEPTION(Foundation_API, StackOverflowException, RuntimeException)
237POCO_DECLARE_EXCEPTION(Foundation_API, ArithmeticException, RuntimeException)
238
239POCO_DECLARE_EXCEPTION(Foundation_API, DataFormatException, DataException)
240POCO_DECLARE_EXCEPTION(Foundation_API, SyntaxException, DataException)
241POCO_DECLARE_EXCEPTION(Foundation_API, CircularReferenceException, DataException)
242POCO_DECLARE_EXCEPTION(Foundation_API, PathSyntaxException, SyntaxException)
243POCO_DECLARE_EXCEPTION(Foundation_API, IOException, RuntimeException)
244POCO_DECLARE_EXCEPTION(Foundation_API, ProtocolException, IOException)
245POCO_DECLARE_EXCEPTION(Foundation_API, FileException, IOException)
246POCO_DECLARE_EXCEPTION(Foundation_API, FileExistsException, FileException)
247POCO_DECLARE_EXCEPTION(Foundation_API, FileNotFoundException, FileException)
248POCO_DECLARE_EXCEPTION(Foundation_API, PathNotFoundException, FileException)
249POCO_DECLARE_EXCEPTION(Foundation_API, FileReadOnlyException, FileException)
250POCO_DECLARE_EXCEPTION(Foundation_API, FileAccessDeniedException, FileException)
251POCO_DECLARE_EXCEPTION(Foundation_API, CreateFileException, FileException)
252POCO_DECLARE_EXCEPTION(Foundation_API, OpenFileException, FileException)
253POCO_DECLARE_EXCEPTION(Foundation_API, WriteFileException, FileException)
254POCO_DECLARE_EXCEPTION(Foundation_API, ReadFileException, FileException)
255POCO_DECLARE_EXCEPTION(Foundation_API, DirectoryNotEmptyException, FileException)
256POCO_DECLARE_EXCEPTION(Foundation_API, UnknownURISchemeException, RuntimeException)
257POCO_DECLARE_EXCEPTION(Foundation_API, TooManyURIRedirectsException, RuntimeException)
258POCO_DECLARE_EXCEPTION(Foundation_API, URISyntaxException, SyntaxException)
259
260POCO_DECLARE_EXCEPTION(Foundation_API, ApplicationException, Exception)
261POCO_DECLARE_EXCEPTION(Foundation_API, BadCastException, RuntimeException)
262
263
264} // namespace Poco
265
266
267#endif // Foundation_Exception_INCLUDED
268