exception.hh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. * Copyright 2005 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef EXCEPTION_HH
  17. #define EXCEPTION_HH
  18. #include <exception>
  19. #include <iostream>
  20. #include <string>
  21. namespace hadoop {
  22. /**
  23. * Parent-type for all exceptions in hadoop.
  24. * Provides an application specified message to the user, a call stack from
  25. * where the exception was created, and optionally an exception that caused
  26. * this one.
  27. */
  28. class Exception: public std::exception {
  29. public:
  30. /**
  31. * Create an exception.
  32. * @param message The message to give to the user.
  33. * @param reason The exception that caused the new exception.
  34. */
  35. explicit Exception(const std::string& message,
  36. const std::string& component="",
  37. const std::string& location="",
  38. const Exception* reason=NULL);
  39. /**
  40. * Copy the exception.
  41. * Clones the reason, if there is one.
  42. */
  43. Exception(const Exception&);
  44. virtual ~Exception() throw ();
  45. /**
  46. * Make a new copy of the given exception by dynamically allocating
  47. * memory.
  48. */
  49. virtual Exception* clone() const;
  50. /**
  51. * Print all of the information about the exception.
  52. */
  53. virtual void print(std::ostream& stream=std::cerr) const;
  54. /**
  55. * Result of print() as a string.
  56. */
  57. virtual std::string toString() const;
  58. /**
  59. * Print the call stack where the exception was created.
  60. */
  61. virtual void printCallStack(std::ostream& stream=std::cerr) const;
  62. const std::string& getMessage() const {
  63. return mMessage;
  64. }
  65. const std::string& getComponent() const {
  66. return mComponent;
  67. }
  68. const std::string& getLocation() const {
  69. return mLocation;
  70. }
  71. const Exception* getReason() const {
  72. return mReason;
  73. }
  74. /**
  75. * Provide a body for the virtual from std::exception.
  76. */
  77. virtual const char* what() const throw () {
  78. return mMessage.c_str();
  79. }
  80. virtual const char* getTypename() const;
  81. private:
  82. const static int sMaxCallStackDepth = 10;
  83. const std::string mMessage;
  84. const std::string mComponent;
  85. const std::string mLocation;
  86. int mCalls;
  87. void* mCallStack[sMaxCallStackDepth];
  88. const Exception* mReason;
  89. // NOT IMPLEMENTED
  90. std::exception& operator=(const std::exception& right) throw ();
  91. };
  92. class IOException: public Exception {
  93. public:
  94. IOException(const std::string& message,
  95. const std::string& component="",
  96. const std::string& location="",
  97. const Exception* reason = NULL);
  98. virtual IOException* clone() const;
  99. virtual const char* getTypename() const;
  100. };
  101. }
  102. #endif