exceptions.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. * Copyright Andrey Semashev 2007 - 2015.
  3. * Distributed under the Boost Software License, Version 1.0.
  4. * (See accompanying file LICENSE_1_0.txt or copy at
  5. * http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. /*!
  8. * \file
  9. * \author Andrey Semashev
  10. * \date 31.10.2009
  11. *
  12. * The header contains exception classes declarations.
  13. */
  14. #ifndef BOOST_LOG_EXCEPTIONS_HPP_INCLUDED_
  15. #define BOOST_LOG_EXCEPTIONS_HPP_INCLUDED_
  16. #include <cstddef>
  17. #include <string>
  18. #include <stdexcept>
  19. #include <boost/type_index.hpp>
  20. #include <boost/preprocessor/seq/enum.hpp>
  21. #include <boost/system/error_code.hpp>
  22. #include <boost/system/system_error.hpp>
  23. #include <boost/log/detail/config.hpp>
  24. #include <boost/log/attributes/attribute_name.hpp>
  25. #include <boost/log/detail/header.hpp>
  26. #ifdef BOOST_HAS_PRAGMA_ONCE
  27. #pragma once
  28. #endif
  29. namespace boost {
  30. // Forward-declaration of an exception base class from Boost.Exception
  31. #if defined(__GNUC__)
  32. # if (__GNUC__ == 4 && __GNUC_MINOR__ >= 1) || (__GNUC__ > 4)
  33. # pragma GCC visibility push (default)
  34. class exception;
  35. # pragma GCC visibility pop
  36. # else
  37. class exception;
  38. # endif
  39. #else
  40. class BOOST_SYMBOL_VISIBLE exception;
  41. #endif
  42. BOOST_LOG_OPEN_NAMESPACE
  43. namespace aux {
  44. //! Attaches attribute name exception information
  45. BOOST_LOG_API void attach_attribute_name_info(exception& e, attribute_name const& name);
  46. } // namespace aux
  47. /*!
  48. * \brief Base class for memory allocation errors
  49. *
  50. * Exceptions derived from this class indicate problems with memory allocation.
  51. */
  52. class BOOST_LOG_API bad_alloc :
  53. public std::bad_alloc
  54. {
  55. private:
  56. std::string m_message;
  57. public:
  58. /*!
  59. * Initializing constructor. Creates an exception with the specified error message.
  60. */
  61. explicit bad_alloc(const char* descr);
  62. /*!
  63. * Initializing constructor. Creates an exception with the specified error message.
  64. */
  65. explicit bad_alloc(std::string const& descr);
  66. /*!
  67. * Destructor
  68. */
  69. ~bad_alloc() throw();
  70. /*!
  71. * Error message accessor.
  72. */
  73. const char* what() const throw();
  74. #ifndef BOOST_LOG_DOXYGEN_PASS
  75. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line, const char* descr);
  76. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line, std::string const& descr);
  77. #endif
  78. };
  79. /*!
  80. * \brief The exception is used to indicate reaching a storage capacity limit
  81. */
  82. class BOOST_LOG_API capacity_limit_reached :
  83. public bad_alloc
  84. {
  85. public:
  86. /*!
  87. * Initializing constructor. Creates an exception with the specified error message.
  88. */
  89. explicit capacity_limit_reached(const char* descr);
  90. /*!
  91. * Initializing constructor. Creates an exception with the specified error message.
  92. */
  93. explicit capacity_limit_reached(std::string const& descr);
  94. /*!
  95. * Destructor
  96. */
  97. ~capacity_limit_reached() throw();
  98. #ifndef BOOST_LOG_DOXYGEN_PASS
  99. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line, const char* descr);
  100. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line, std::string const& descr);
  101. #endif
  102. };
  103. /*!
  104. * \brief Base class for runtime exceptions from the logging library
  105. *
  106. * Exceptions derived from this class indicate a problem that may not directly
  107. * be caused by the user's code that interacts with the library, such as
  108. * errors caused by input data.
  109. */
  110. class BOOST_LOG_API runtime_error :
  111. public std::runtime_error
  112. {
  113. public:
  114. /*!
  115. * Initializing constructor. Creates an exception with the specified error message.
  116. */
  117. explicit runtime_error(std::string const& descr);
  118. /*!
  119. * Destructor
  120. */
  121. ~runtime_error() throw();
  122. };
  123. /*!
  124. * \brief Exception class that is used to indicate errors of missing values
  125. */
  126. class BOOST_LOG_API missing_value :
  127. public runtime_error
  128. {
  129. public:
  130. /*!
  131. * Default constructor. Creates an exception with the default error message.
  132. */
  133. missing_value();
  134. /*!
  135. * Initializing constructor. Creates an exception with the specified error message.
  136. */
  137. explicit missing_value(std::string const& descr);
  138. /*!
  139. * Destructor
  140. */
  141. ~missing_value() throw();
  142. #ifndef BOOST_LOG_DOXYGEN_PASS
  143. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line);
  144. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line, const char* descr);
  145. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line, std::string const& descr);
  146. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line, const char* descr, attribute_name const& name);
  147. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line, std::string const& descr, attribute_name const& name);
  148. #endif
  149. };
  150. /*!
  151. * \brief Exception class that is used to indicate errors of incorrect type of an object
  152. */
  153. class BOOST_LOG_API invalid_type :
  154. public runtime_error
  155. {
  156. public:
  157. /*!
  158. * Default constructor. Creates an exception with the default error message.
  159. */
  160. invalid_type();
  161. /*!
  162. * Initializing constructor. Creates an exception with the specified error message.
  163. */
  164. explicit invalid_type(std::string const& descr);
  165. /*!
  166. * Destructor
  167. */
  168. ~invalid_type() throw();
  169. #ifndef BOOST_LOG_DOXYGEN_PASS
  170. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line);
  171. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line, const char* descr);
  172. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line, std::string const& descr);
  173. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line, const char* descr, attribute_name const& name);
  174. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line, std::string const& descr, attribute_name const& name);
  175. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line, const char* descr, typeindex::type_index const& type);
  176. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line, std::string const& descr, typeindex::type_index const& type);
  177. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line, const char* descr, attribute_name const& name, typeindex::type_index const& type);
  178. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line, std::string const& descr, attribute_name const& name, typeindex::type_index const& type);
  179. #endif
  180. };
  181. /*!
  182. * \brief Exception class that is used to indicate errors of incorrect value of an object
  183. */
  184. class BOOST_LOG_API invalid_value :
  185. public runtime_error
  186. {
  187. public:
  188. /*!
  189. * Default constructor. Creates an exception with the default error message.
  190. */
  191. invalid_value();
  192. /*!
  193. * Initializing constructor. Creates an exception with the specified error message.
  194. */
  195. explicit invalid_value(std::string const& descr);
  196. /*!
  197. * Destructor
  198. */
  199. ~invalid_value() throw();
  200. #ifndef BOOST_LOG_DOXYGEN_PASS
  201. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line);
  202. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line, const char* descr);
  203. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line, std::string const& descr);
  204. #endif
  205. };
  206. /*!
  207. * \brief Exception class that is used to indicate parsing errors
  208. */
  209. class BOOST_LOG_API parse_error :
  210. public runtime_error
  211. {
  212. public:
  213. /*!
  214. * Default constructor. Creates an exception with the default error message.
  215. */
  216. parse_error();
  217. /*!
  218. * Initializing constructor. Creates an exception with the specified error message.
  219. */
  220. explicit parse_error(std::string const& descr);
  221. /*!
  222. * Destructor
  223. */
  224. ~parse_error() throw();
  225. #ifndef BOOST_LOG_DOXYGEN_PASS
  226. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line);
  227. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line, const char* descr);
  228. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line, std::string const& descr);
  229. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line, const char* descr, std::size_t content_line);
  230. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line, std::string const& descr, std::size_t content_line);
  231. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line, const char* descr, attribute_name const& name);
  232. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line, std::string const& descr, attribute_name const& name);
  233. #endif
  234. };
  235. /*!
  236. * \brief Exception class that is used to indicate conversion errors
  237. */
  238. class BOOST_LOG_API conversion_error :
  239. public runtime_error
  240. {
  241. public:
  242. /*!
  243. * Default constructor. Creates an exception with the default error message.
  244. */
  245. conversion_error();
  246. /*!
  247. * Initializing constructor. Creates an exception with the specified error message.
  248. */
  249. explicit conversion_error(std::string const& descr);
  250. /*!
  251. * Destructor
  252. */
  253. ~conversion_error() throw();
  254. #ifndef BOOST_LOG_DOXYGEN_PASS
  255. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line);
  256. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line, const char* descr);
  257. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line, std::string const& descr);
  258. #endif
  259. };
  260. /*!
  261. * \brief Exception class that is used to indicate underlying OS API errors
  262. */
  263. class BOOST_LOG_API system_error :
  264. public boost::system::system_error
  265. {
  266. public:
  267. /*!
  268. * Initializing constructor. Creates an exception with the specified error message.
  269. */
  270. system_error(boost::system::error_code code, std::string const& descr);
  271. /*!
  272. * Destructor
  273. */
  274. ~system_error() throw();
  275. #ifndef BOOST_LOG_DOXYGEN_PASS
  276. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line, const char* descr, int system_error_code);
  277. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line, std::string const& descr, int system_error_code);
  278. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line, const char* descr, boost::system::error_code code);
  279. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line, std::string const& descr, boost::system::error_code code);
  280. #endif
  281. };
  282. /*!
  283. * \brief Base class for logic exceptions from the logging library
  284. *
  285. * Exceptions derived from this class usually indicate errors on the user's side, such as
  286. * incorrect library usage.
  287. */
  288. class BOOST_LOG_API logic_error :
  289. public std::logic_error
  290. {
  291. public:
  292. /*!
  293. * Initializing constructor. Creates an exception with the specified error message.
  294. */
  295. explicit logic_error(std::string const& descr);
  296. /*!
  297. * Destructor
  298. */
  299. ~logic_error() throw();
  300. #ifndef BOOST_LOG_DOXYGEN_PASS
  301. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line, const char* descr);
  302. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line, std::string const& descr);
  303. #endif
  304. };
  305. /*!
  306. * \brief Exception class that is used to indicate ODR violation
  307. */
  308. class BOOST_LOG_API odr_violation :
  309. public logic_error
  310. {
  311. public:
  312. /*!
  313. * Default constructor. Creates an exception with the default error message.
  314. */
  315. odr_violation();
  316. /*!
  317. * Initializing constructor. Creates an exception with the specified error message.
  318. */
  319. explicit odr_violation(std::string const& descr);
  320. /*!
  321. * Destructor
  322. */
  323. ~odr_violation() throw();
  324. #ifndef BOOST_LOG_DOXYGEN_PASS
  325. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line);
  326. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line, const char* descr);
  327. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line, std::string const& descr);
  328. #endif
  329. };
  330. /*!
  331. * \brief Exception class that is used to indicate invalid call sequence
  332. */
  333. class BOOST_LOG_API unexpected_call :
  334. public logic_error
  335. {
  336. public:
  337. /*!
  338. * Default constructor. Creates an exception with the default error message.
  339. */
  340. unexpected_call();
  341. /*!
  342. * Initializing constructor. Creates an exception with the specified error message.
  343. */
  344. explicit unexpected_call(std::string const& descr);
  345. /*!
  346. * Destructor
  347. */
  348. ~unexpected_call() throw();
  349. #ifndef BOOST_LOG_DOXYGEN_PASS
  350. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line);
  351. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line, const char* descr);
  352. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line, std::string const& descr);
  353. #endif
  354. };
  355. /*!
  356. * \brief Exception class that is used to indicate invalid library setup
  357. */
  358. class BOOST_LOG_API setup_error :
  359. public logic_error
  360. {
  361. public:
  362. /*!
  363. * Default constructor. Creates an exception with the default error message.
  364. */
  365. setup_error();
  366. /*!
  367. * Initializing constructor. Creates an exception with the specified error message.
  368. */
  369. explicit setup_error(std::string const& descr);
  370. /*!
  371. * Destructor
  372. */
  373. ~setup_error() throw();
  374. #ifndef BOOST_LOG_DOXYGEN_PASS
  375. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line);
  376. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line, const char* descr);
  377. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line, std::string const& descr);
  378. #endif
  379. };
  380. /*!
  381. * \brief Exception class that is used to indicate library limitation
  382. */
  383. class BOOST_LOG_API limitation_error :
  384. public logic_error
  385. {
  386. public:
  387. /*!
  388. * Default constructor. Creates an exception with the default error message.
  389. */
  390. limitation_error();
  391. /*!
  392. * Initializing constructor. Creates an exception with the specified error message.
  393. */
  394. explicit limitation_error(std::string const& descr);
  395. /*!
  396. * Destructor
  397. */
  398. ~limitation_error() throw();
  399. #ifndef BOOST_LOG_DOXYGEN_PASS
  400. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line);
  401. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line, const char* descr);
  402. static BOOST_LOG_NORETURN void throw_(const char* file, std::size_t line, std::string const& descr);
  403. #endif
  404. };
  405. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  406. } // namespace boost
  407. #ifndef BOOST_LOG_DOXYGEN_PASS
  408. #define BOOST_LOG_THROW(ex)\
  409. ex::throw_(__FILE__, static_cast< std::size_t >(__LINE__))
  410. #define BOOST_LOG_THROW_DESCR(ex, descr)\
  411. ex::throw_(__FILE__, static_cast< std::size_t >(__LINE__), descr)
  412. #define BOOST_LOG_THROW_DESCR_PARAMS(ex, descr, params)\
  413. ex::throw_(__FILE__, static_cast< std::size_t >(__LINE__), descr, BOOST_PP_SEQ_ENUM(params))
  414. #endif // BOOST_LOG_DOXYGEN_PASS
  415. #include <boost/log/detail/footer.hpp>
  416. #endif // BOOST_LOG_EXCEPTIONS_HPP_INCLUDED_