Exception.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. #ifndef Foundation_Exception_INCLUDED
  16. #define Foundation_Exception_INCLUDED
  17. #include "Poco/Foundation.h"
  18. #include <stdexcept>
  19. namespace Poco {
  20. class Foundation_API Exception: public std::exception
  21. /// This is the base class for all exceptions defined
  22. /// in the Poco class library.
  23. {
  24. public:
  25. Exception(const std::string& msg, int code = 0);
  26. /// Creates an exception.
  27. Exception(const std::string& msg, const std::string& arg, int code = 0);
  28. /// Creates an exception.
  29. Exception(const std::string& msg, const Exception& nested, int code = 0);
  30. /// Creates an exception and stores a clone
  31. /// of the nested exception.
  32. Exception(const Exception& exc);
  33. /// Copy constructor.
  34. ~Exception() throw();
  35. /// Destroys the exception and deletes the nested exception.
  36. Exception& operator = (const Exception& exc);
  37. /// Assignment operator.
  38. virtual const char* name() const throw();
  39. /// Returns a static string describing the exception.
  40. virtual const char* className() const throw();
  41. /// Returns the name of the exception class.
  42. virtual const char* what() const throw();
  43. /// Returns a static string describing the exception.
  44. ///
  45. /// Same as name(), but for compatibility with std::exception.
  46. const Exception* nested() const;
  47. /// Returns a pointer to the nested exception, or
  48. /// null if no nested exception exists.
  49. const std::string& message() const;
  50. /// Returns the message text.
  51. int code() const;
  52. /// Returns the exception code if defined.
  53. std::string displayText() const;
  54. /// Returns a string consisting of the
  55. /// message name and the message text.
  56. virtual Exception* clone() const;
  57. /// Creates an exact copy of the exception.
  58. ///
  59. /// The copy can later be thrown again by
  60. /// invoking rethrow() on it.
  61. virtual void rethrow() const;
  62. /// (Re)Throws the exception.
  63. ///
  64. /// This is useful for temporarily storing a
  65. /// copy of an exception (see clone()), then
  66. /// throwing it again.
  67. protected:
  68. Exception(int code = 0);
  69. /// Standard constructor.
  70. void message(const std::string& msg);
  71. /// Sets the message for the exception.
  72. void extendedMessage(const std::string& arg);
  73. /// Sets the extended message for the exception.
  74. private:
  75. std::string _msg;
  76. Exception* _pNested;
  77. int _code;
  78. };
  79. //
  80. // inlines
  81. //
  82. inline const Exception* Exception::nested() const
  83. {
  84. return _pNested;
  85. }
  86. inline const std::string& Exception::message() const
  87. {
  88. return _msg;
  89. }
  90. inline void Exception::message(const std::string& msg)
  91. {
  92. _msg = msg;
  93. }
  94. inline int Exception::code() const
  95. {
  96. return _code;
  97. }
  98. //
  99. // Macros for quickly declaring and implementing exception classes.
  100. // Unfortunately, we cannot use a template here because character
  101. // pointers (which we need for specifying the exception name)
  102. // are not allowed as template arguments.
  103. //
  104. #define POCO_DECLARE_EXCEPTION_CODE(API, CLS, BASE, CODE) \
  105. class API CLS: public BASE \
  106. { \
  107. public: \
  108. CLS(int code = CODE); \
  109. CLS(const std::string& msg, int code = CODE); \
  110. CLS(const std::string& msg, const std::string& arg, int code = CODE); \
  111. CLS(const std::string& msg, const Poco::Exception& exc, int code = CODE); \
  112. CLS(const CLS& exc); \
  113. ~CLS() throw(); \
  114. CLS& operator = (const CLS& exc); \
  115. const char* name() const throw(); \
  116. const char* className() const throw(); \
  117. Poco::Exception* clone() const; \
  118. void rethrow() const; \
  119. };
  120. #define POCO_DECLARE_EXCEPTION(API, CLS, BASE) \
  121. POCO_DECLARE_EXCEPTION_CODE(API, CLS, BASE, 0)
  122. #define POCO_IMPLEMENT_EXCEPTION(CLS, BASE, NAME) \
  123. CLS::CLS(int code): BASE(code) \
  124. { \
  125. } \
  126. CLS::CLS(const std::string& msg, int code): BASE(msg, code) \
  127. { \
  128. } \
  129. CLS::CLS(const std::string& msg, const std::string& arg, int code): BASE(msg, arg, code) \
  130. { \
  131. } \
  132. CLS::CLS(const std::string& msg, const Poco::Exception& exc, int code): BASE(msg, exc, code) \
  133. { \
  134. } \
  135. CLS::CLS(const CLS& exc): BASE(exc) \
  136. { \
  137. } \
  138. CLS::~CLS() throw() \
  139. { \
  140. } \
  141. CLS& CLS::operator = (const CLS& exc) \
  142. { \
  143. BASE::operator = (exc); \
  144. return *this; \
  145. } \
  146. const char* CLS::name() const throw() \
  147. { \
  148. return NAME; \
  149. } \
  150. const char* CLS::className() const throw() \
  151. { \
  152. return typeid(*this).name(); \
  153. } \
  154. Poco::Exception* CLS::clone() const \
  155. { \
  156. return new CLS(*this); \
  157. } \
  158. void CLS::rethrow() const \
  159. { \
  160. throw *this; \
  161. }
  162. //
  163. // Standard exception classes
  164. //
  165. POCO_DECLARE_EXCEPTION(Foundation_API, LogicException, Exception)
  166. POCO_DECLARE_EXCEPTION(Foundation_API, AssertionViolationException, LogicException)
  167. POCO_DECLARE_EXCEPTION(Foundation_API, NullPointerException, LogicException)
  168. POCO_DECLARE_EXCEPTION(Foundation_API, NullValueException, LogicException)
  169. POCO_DECLARE_EXCEPTION(Foundation_API, BugcheckException, LogicException)
  170. POCO_DECLARE_EXCEPTION(Foundation_API, InvalidArgumentException, LogicException)
  171. POCO_DECLARE_EXCEPTION(Foundation_API, NotImplementedException, LogicException)
  172. POCO_DECLARE_EXCEPTION(Foundation_API, RangeException, LogicException)
  173. POCO_DECLARE_EXCEPTION(Foundation_API, IllegalStateException, LogicException)
  174. POCO_DECLARE_EXCEPTION(Foundation_API, InvalidAccessException, LogicException)
  175. POCO_DECLARE_EXCEPTION(Foundation_API, SignalException, LogicException)
  176. POCO_DECLARE_EXCEPTION(Foundation_API, UnhandledException, LogicException)
  177. POCO_DECLARE_EXCEPTION(Foundation_API, RuntimeException, Exception)
  178. POCO_DECLARE_EXCEPTION(Foundation_API, NotFoundException, RuntimeException)
  179. POCO_DECLARE_EXCEPTION(Foundation_API, ExistsException, RuntimeException)
  180. POCO_DECLARE_EXCEPTION(Foundation_API, TimeoutException, RuntimeException)
  181. POCO_DECLARE_EXCEPTION(Foundation_API, SystemException, RuntimeException)
  182. POCO_DECLARE_EXCEPTION(Foundation_API, RegularExpressionException, RuntimeException)
  183. POCO_DECLARE_EXCEPTION(Foundation_API, LibraryLoadException, RuntimeException)
  184. POCO_DECLARE_EXCEPTION(Foundation_API, LibraryAlreadyLoadedException, RuntimeException)
  185. POCO_DECLARE_EXCEPTION(Foundation_API, NoThreadAvailableException, RuntimeException)
  186. POCO_DECLARE_EXCEPTION(Foundation_API, PropertyNotSupportedException, RuntimeException)
  187. POCO_DECLARE_EXCEPTION(Foundation_API, PoolOverflowException, RuntimeException)
  188. POCO_DECLARE_EXCEPTION(Foundation_API, NoPermissionException, RuntimeException)
  189. POCO_DECLARE_EXCEPTION(Foundation_API, OutOfMemoryException, RuntimeException)
  190. POCO_DECLARE_EXCEPTION(Foundation_API, DataException, RuntimeException)
  191. POCO_DECLARE_EXCEPTION(Foundation_API, DataFormatException, DataException)
  192. POCO_DECLARE_EXCEPTION(Foundation_API, SyntaxException, DataException)
  193. POCO_DECLARE_EXCEPTION(Foundation_API, CircularReferenceException, DataException)
  194. POCO_DECLARE_EXCEPTION(Foundation_API, PathSyntaxException, SyntaxException)
  195. POCO_DECLARE_EXCEPTION(Foundation_API, IOException, RuntimeException)
  196. POCO_DECLARE_EXCEPTION(Foundation_API, ProtocolException, IOException)
  197. POCO_DECLARE_EXCEPTION(Foundation_API, FileException, IOException)
  198. POCO_DECLARE_EXCEPTION(Foundation_API, FileExistsException, FileException)
  199. POCO_DECLARE_EXCEPTION(Foundation_API, FileNotFoundException, FileException)
  200. POCO_DECLARE_EXCEPTION(Foundation_API, PathNotFoundException, FileException)
  201. POCO_DECLARE_EXCEPTION(Foundation_API, FileReadOnlyException, FileException)
  202. POCO_DECLARE_EXCEPTION(Foundation_API, FileAccessDeniedException, FileException)
  203. POCO_DECLARE_EXCEPTION(Foundation_API, CreateFileException, FileException)
  204. POCO_DECLARE_EXCEPTION(Foundation_API, OpenFileException, FileException)
  205. POCO_DECLARE_EXCEPTION(Foundation_API, WriteFileException, FileException)
  206. POCO_DECLARE_EXCEPTION(Foundation_API, ReadFileException, FileException)
  207. POCO_DECLARE_EXCEPTION(Foundation_API, DirectoryNotEmptyException, FileException)
  208. POCO_DECLARE_EXCEPTION(Foundation_API, UnknownURISchemeException, RuntimeException)
  209. POCO_DECLARE_EXCEPTION(Foundation_API, TooManyURIRedirectsException, RuntimeException)
  210. POCO_DECLARE_EXCEPTION(Foundation_API, URISyntaxException, SyntaxException)
  211. POCO_DECLARE_EXCEPTION(Foundation_API, ApplicationException, Exception)
  212. POCO_DECLARE_EXCEPTION(Foundation_API, BadCastException, RuntimeException)
  213. } // namespace Poco
  214. #endif // Foundation_Exception_INCLUDED