exceptions.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Copyright (C) 2009 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef __EXCEPTIONS_H
  15. #define __EXCEPTIONS_H 1
  16. #include <stdexcept>
  17. #include <string>
  18. #include <sstream>
  19. namespace isc {
  20. ///
  21. /// This is a base class for exceptions thrown from the DNS library module.
  22. /// Normally, the exceptions are thrown via a convenient shortcut macro,
  23. /// @ref isc_throw, which automatically gives trivial parameters for the
  24. /// exception such as the file name and line number where the exception is
  25. /// triggered.
  26. ///
  27. class Exception : public std::exception {
  28. public:
  29. ///
  30. /// \name Constructors and Destructor
  31. ///
  32. //@{
  33. /// \brief Constructor for a given type for exceptions with file name and
  34. /// file line number.
  35. ///
  36. /// @param file the file name where the exception was thrown.
  37. /// @param line the line in \a file where the exception was thrown.
  38. /// @param what a description (type) of the exception.
  39. Exception(const char* file, size_t line, const char* what) :
  40. file_(file), line_(line), what_(what) {}
  41. /// \brief Constructor for a given type for exceptions with file name and
  42. /// file line number.
  43. ///
  44. /// @param file the file name where the exception was thrown.
  45. /// @param line the line in \a file where the exception was thrown.
  46. /// @param what a description (type) of the exception.
  47. Exception(const char* file, size_t line, const std::string& what) :
  48. file_(file), line_(line), what_(what) {}
  49. /// The destructor
  50. virtual ~Exception() throw() {}
  51. //@}
  52. private:
  53. ///
  54. /// The assignment operator is intentionally disabled.
  55. ///
  56. void operator=(const Exception& src);
  57. public:
  58. ///
  59. /// \name Methods Reimplemented against the Standard Exception Class
  60. ///
  61. //@{
  62. /// \brief Returns a C-style character string of the cause of the exception.
  63. ///
  64. /// Note: we normally don't use exception specifications, but this is an
  65. /// "exception" to that policy as it's enforced by the base class.
  66. ///
  67. /// @return A C-style character string of the exception cause.
  68. virtual const char* what() const throw();
  69. //@}
  70. ///
  71. /// \name Getter Methods
  72. ///
  73. //@{
  74. /// \brief Gets a string describing the cause of the exception.
  75. ///
  76. /// @return the cause string.
  77. const std::string& getMessage() const { return (what_); }
  78. /// \brief Gets the file name where the exception was thrown.
  79. ///
  80. /// @return a C-style string of the file name.
  81. const char* getFile() const { return (file_); }
  82. /// \brief Gets the line number of the file where the exception was thrown.
  83. ///
  84. /// @return an integer specifying the line number.
  85. size_t getLine() const { return (line_); }
  86. //@}
  87. private:
  88. const char* const file_;
  89. size_t line_;
  90. const std::string what_;
  91. };
  92. /// \brief A generic exception that is thrown if a parameter given
  93. /// to a method would refer to or modify out-of-range data.
  94. class OutOfRange : public Exception {
  95. public:
  96. OutOfRange(const char* file, size_t line, const char* what) :
  97. isc::Exception(file, line, what) {}
  98. };
  99. /// \brief A generic exception that is thrown if a parameter given
  100. /// to a method or function is considered invalid and no other specific
  101. /// exceptions are suitable to describe the error.
  102. class InvalidParameter : public Exception {
  103. public:
  104. InvalidParameter(const char* file, size_t line, const char* what) :
  105. isc::Exception(file, line, what) {}
  106. };
  107. /// \brief A generic exception that is thrown if a parameter given
  108. /// to a method is considered invalid in that context.
  109. class BadValue : public Exception {
  110. public:
  111. BadValue(const char* file, size_t line, const char* what) :
  112. isc::Exception(file, line, what) {}
  113. };
  114. ///
  115. /// \brief A generic exception that is thrown when an unexpected
  116. /// error condition occurs.
  117. ///
  118. class Unexpected : public Exception {
  119. public:
  120. Unexpected(const char* file, size_t line, const char* what) :
  121. isc::Exception(file, line, what) {}
  122. };
  123. ///
  124. /// A shortcut macro to insert known values into exception arguments.
  125. ///
  126. /// It allows the \c stream argument to be part of a statement using an
  127. /// \c ostream object and its \c operator<<. For example,
  128. /// \code int x = 10;
  129. /// isc_throw(SomeException, "Error happened, parameter: " << x);
  130. /// \endcode
  131. /// will throw an exception of class \c SomeException whose \c what string
  132. /// will be <code>"Error happened, parameter: 10"</code>.
  133. ///
  134. /// Note: the stream related operations or creation of the exception object
  135. /// may itself throw an exception (specifically \c std::bad_alloc).
  136. /// Even though it should be very rare, we may have to address this issue later.
  137. ///
  138. /// Note: in general we hate macros and avoid using it in the code. This is
  139. /// one of few exceptions to that policy. inline functions cannot be used
  140. /// for embedding \c __FILE__ and \c __LINE__. This is the main reason why
  141. /// this is defined as a macro. The convenience for the ostream is a secondary
  142. /// purpose (if that were the only possible reason we should rather avoid
  143. /// using a macro).
  144. #define isc_throw(type, stream) \
  145. do { \
  146. std::ostringstream oss__; \
  147. oss__ << stream; \
  148. throw type(__FILE__, __LINE__, oss__.str().c_str()); \
  149. } while (1)
  150. ///
  151. /// Similar as isc_throw, but allows the exception to have one additional
  152. /// parameter (the stream/text goes first)
  153. #define isc_throw_1(type, stream, param1) \
  154. do { \
  155. std::ostringstream oss__; \
  156. oss__ << stream; \
  157. throw type(__FILE__, __LINE__, oss__.str().c_str(), param1); \
  158. } while (1)
  159. }
  160. #endif // __EXCEPTIONS_H
  161. // Local Variables:
  162. // mode: c++
  163. // End: