exceptions.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. 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 dns_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 @ref 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 @ref 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. ///
  93. /// \brief A standard DNS module exception that is thrown if a parameter give
  94. /// to a method would refer to or modify out-of-range data.
  95. ///
  96. class OutOfRange : public Exception {
  97. public:
  98. OutOfRange(const char* file, size_t line, const char* what) :
  99. isc::Exception(file, line, what) {}
  100. };
  101. ///
  102. /// \brief A standard DNS module exception that is thrown when an unexpected
  103. /// error condition occurs.
  104. ///
  105. class Unexpected : public Exception {
  106. public:
  107. Unexpected(const char* file, size_t line, const char* what) :
  108. isc::Exception(file, line, what) {}
  109. };
  110. ///
  111. /// A shortcut macro to insert known values into exception arguments.
  112. ///
  113. #define dns_throw(type, args...) throw type(__FILE__, __LINE__, args)
  114. }
  115. #endif // __EXCEPTIONS_H
  116. // Local Variables:
  117. // mode: c++
  118. // End: