Exception.h 8.9 KB

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