exceptions.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. // $Id$
  15. #ifndef __EXCEPTIONS_H
  16. #define __EXCEPTIONS_H 1
  17. #include <stdexcept>
  18. #include <string>
  19. #include <sstream>
  20. namespace isc {
  21. ///
  22. /// This is a base class for exceptions thrown from the DNS library module.
  23. /// Normally, the exceptions are thrown via a convenient shortcut macro,
  24. /// @ref isc_throw, which automatically gives trivial parameters for the
  25. /// exception such as the file name and line number where the exception is
  26. /// triggered.
  27. ///
  28. class Exception : public std::exception {
  29. public:
  30. ///
  31. /// \name Constructors and Destructor
  32. ///
  33. //@{
  34. /// \brief Constructor for a given type for exceptions with file name and
  35. /// file line number.
  36. ///
  37. /// @param file the file name where the exception was thrown.
  38. /// @param line the line in \a file where the exception was thrown.
  39. /// @param what a description (type) of the exception.
  40. Exception(const char* file, size_t line, const char* what) :
  41. file_(file), line_(line), what_(what) {}
  42. /// \brief Constructor for a given type for exceptions with file name and
  43. /// file line number.
  44. ///
  45. /// @param file the file name where the exception was thrown.
  46. /// @param line the line in \a file where the exception was thrown.
  47. /// @param what a description (type) of the exception.
  48. Exception(const char* file, size_t line, const std::string& what) :
  49. file_(file), line_(line), what_(what) {}
  50. /// The destructor
  51. virtual ~Exception() throw() {}
  52. //@}
  53. private:
  54. ///
  55. /// The assignment operator is intentionally disabled.
  56. ///
  57. void operator=(const Exception& src);
  58. public:
  59. ///
  60. /// \name Methods Reimplemented against the Standard Exception Class
  61. ///
  62. //@{
  63. /// \brief Returns a C-style character string of the cause of the exception.
  64. ///
  65. /// Note: we normally don't use exception specifications, but this is an
  66. /// "exception" to that policy as it's enforced by the base class.
  67. ///
  68. /// @return A C-style character string of the exception cause.
  69. virtual const char* what() const throw();
  70. //@}
  71. ///
  72. /// \name Getter Methods
  73. ///
  74. //@{
  75. /// \brief Gets a string describing the cause of the exception.
  76. ///
  77. /// @return the cause string.
  78. const std::string& getMessage() const { return (what_); }
  79. /// \brief Gets the file name where the exception was thrown.
  80. ///
  81. /// @return a C-style string of the file name.
  82. const char* getFile() const { return (file_); }
  83. /// \brief Gets the line number of the file where the exception was thrown.
  84. ///
  85. /// @return an integer specifying the line number.
  86. size_t getLine() const { return (line_); }
  87. //@}
  88. private:
  89. const char* const file_;
  90. size_t line_;
  91. const std::string what_;
  92. };
  93. /// \brief A generic exception that is thrown if a parameter given
  94. /// to a method would refer to or modify out-of-range data.
  95. class OutOfRange : public Exception {
  96. public:
  97. OutOfRange(const char* file, size_t line, const char* what) :
  98. isc::Exception(file, line, what) {}
  99. };
  100. /// \brief A generic exception that is thrown if a parameter given
  101. /// to a method or function is considered invalid and no other specific
  102. /// exceptions are suitable to describe the error.
  103. class InvalidParameter : public Exception {
  104. public:
  105. InvalidParameter(const char* file, size_t line, const char* what) :
  106. isc::Exception(file, line, what) {}
  107. };
  108. /// \brief A generic exception that is thrown if a parameter given
  109. /// to a method is considered invalid in that context.
  110. class BadValue : public Exception {
  111. public:
  112. BadValue(const char* file, size_t line, const char* what) :
  113. isc::Exception(file, line, what) {}
  114. };
  115. ///
  116. /// \brief A generic exception that is thrown when an unexpected
  117. /// error condition occurs.
  118. ///
  119. class Unexpected : public Exception {
  120. public:
  121. Unexpected(const char* file, size_t line, const char* what) :
  122. isc::Exception(file, line, what) {}
  123. };
  124. ///
  125. /// A shortcut macro to insert known values into exception arguments.
  126. ///
  127. /// It allows the \c stream argument to be part of a statement using an
  128. /// \c ostream object and its \c operator<<. For example,
  129. /// \code int x = 10;
  130. /// isc_throw(SomeException, "Error happened, parameter: " << x);
  131. /// \endcode
  132. /// will throw an exception of class \c SomeException whose \c what string
  133. /// will be <code>"Error happened, parameter: 10"</code>.
  134. ///
  135. /// Note: the stream related operations or creation of the exception object
  136. /// may itself throw an exception (specifically \c std::bad_alloc).
  137. /// Even though it should be very rare, we may have to address this issue later.
  138. ///
  139. /// Note: in general we hate macros and avoid using it in the code. This is
  140. /// one of few exceptions to that policy. inline functions cannot be used
  141. /// for embedding \c __FILE__ and \c __LINE__. This is the main reason why
  142. /// this is defined as a macro. The convenience for the ostream is a secondary
  143. /// purpose (if that were the only possible reason we should rather avoid
  144. /// using a macro).
  145. #define isc_throw(type, stream) \
  146. do { \
  147. std::ostringstream oss__; \
  148. oss__ << stream; \
  149. throw type(__FILE__, __LINE__, oss__.str().c_str()); \
  150. } while (1)
  151. }
  152. #endif // __EXCEPTIONS_H
  153. // Local Variables:
  154. // mode: c++
  155. // End: