Exception.hpp 11 KB

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