exception.hh 3.4 KB

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