exceptions.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 @ref 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 @ref 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. ///
  94. /// \brief A generic exception that is thrown if a parameter given
  95. /// to a method would refer to or modify out-of-range data.
  96. ///
  97. class OutOfRange : public Exception {
  98. public:
  99. OutOfRange(const char* file, size_t line, const char* what) :
  100. isc::Exception(file, line, what) {}
  101. };
  102. ///
  103. /// \brief A generic exception that is thrown if a parameter given
  104. /// to a method is considered invalid in that context.
  105. ///
  106. class BadValue : public Exception {
  107. public:
  108. BadValue(const char* file, size_t line, const char* what) :
  109. isc::Exception(file, line, what) {}
  110. };
  111. ///
  112. /// \brief A generic exception that is thrown when an unexpected
  113. /// error condition occurs.
  114. ///
  115. class Unexpected : public Exception {
  116. public:
  117. Unexpected(const char* file, size_t line, const char* what) :
  118. isc::Exception(file, line, what) {}
  119. };
  120. ///
  121. /// A shortcut macro to insert known values into exception arguments.
  122. ///
  123. /// It allows the \c stream argument to be part of a statement using an
  124. /// \c ostream object and its \c operator<<. For example,
  125. /// \code int x = 10;
  126. /// isc_throw(SomeException, "Error happened, parameter: " << x);
  127. /// \endcode
  128. /// will throw an exception of class \c SomeException whose \c what string
  129. /// will be <code>"Error happened, parameter: 10"</code>.
  130. ///
  131. /// Note: the stream related operations or creation of the exception object
  132. /// may itself throw an exception (specifically \c std::bad_alloc).
  133. /// Even though it should be very rare, we may have to address this issue later.
  134. ///
  135. /// Note: in general we hate macros and avoid using it in the code. This is
  136. /// one of few exceptions to that policy. inline functions cannot be used
  137. /// for embedding \c __FILE__ and \c __LINE__. This is the main reason why
  138. /// this is defined as a macro. The convenience for the ostream is a secondary
  139. /// purpose (if that were the only possible reason we should rather avoid
  140. /// using a macro).
  141. #define isc_throw(type, stream) \
  142. do { \
  143. std::ostringstream oss__; \
  144. oss__ << stream; \
  145. throw type(__FILE__, __LINE__, oss__.str().c_str()); \
  146. } while (1)
  147. }
  148. #endif // __EXCEPTIONS_H
  149. // Local Variables:
  150. // mode: c++
  151. // End: