[120] | 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 | |
---|
| 19 | #ifndef EXCEPTION_HH |
---|
| 20 | #define EXCEPTION_HH |
---|
| 21 | |
---|
| 22 | #include <exception> |
---|
| 23 | #include <iostream> |
---|
| 24 | #include <string> |
---|
| 25 | |
---|
| 26 | namespace hadoop { |
---|
| 27 | |
---|
| 28 | /** |
---|
| 29 | * Parent-type for all exceptions in hadoop. |
---|
| 30 | * Provides an application specified message to the user, a call stack from |
---|
| 31 | * where the exception was created, and optionally an exception that caused |
---|
| 32 | * this one. |
---|
| 33 | */ |
---|
| 34 | class Exception: public std::exception { |
---|
| 35 | public: |
---|
| 36 | |
---|
| 37 | /** |
---|
| 38 | * Create an exception. |
---|
| 39 | * @param message The message to give to the user. |
---|
| 40 | * @param reason The exception that caused the new exception. |
---|
| 41 | */ |
---|
| 42 | explicit Exception(const std::string& message, |
---|
| 43 | const std::string& component="", |
---|
| 44 | const std::string& location="", |
---|
| 45 | const Exception* reason=NULL); |
---|
| 46 | |
---|
| 47 | /** |
---|
| 48 | * Copy the exception. |
---|
| 49 | * Clones the reason, if there is one. |
---|
| 50 | */ |
---|
| 51 | Exception(const Exception&); |
---|
| 52 | |
---|
| 53 | virtual ~Exception() throw (); |
---|
| 54 | |
---|
| 55 | /** |
---|
| 56 | * Make a new copy of the given exception by dynamically allocating |
---|
| 57 | * memory. |
---|
| 58 | */ |
---|
| 59 | virtual Exception* clone() const; |
---|
| 60 | |
---|
| 61 | /** |
---|
| 62 | * Print all of the information about the exception. |
---|
| 63 | */ |
---|
| 64 | virtual void print(std::ostream& stream=std::cerr) const; |
---|
| 65 | |
---|
| 66 | /** |
---|
| 67 | * Result of print() as a string. |
---|
| 68 | */ |
---|
| 69 | virtual std::string toString() const; |
---|
| 70 | |
---|
| 71 | #ifdef USE_EXECINFO |
---|
| 72 | /** |
---|
| 73 | * Print the call stack where the exception was created. |
---|
| 74 | */ |
---|
| 75 | virtual void printCallStack(std::ostream& stream=std::cerr) const; |
---|
| 76 | #endif |
---|
| 77 | |
---|
| 78 | const std::string& getMessage() const { |
---|
| 79 | return mMessage; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | const std::string& getComponent() const { |
---|
| 83 | return mComponent; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | const std::string& getLocation() const { |
---|
| 87 | return mLocation; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | const Exception* getReason() const { |
---|
| 91 | return mReason; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | /** |
---|
| 95 | * Provide a body for the virtual from std::exception. |
---|
| 96 | */ |
---|
| 97 | virtual const char* what() const throw () { |
---|
| 98 | return mMessage.c_str(); |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | virtual const char* getTypename() const; |
---|
| 102 | |
---|
| 103 | private: |
---|
| 104 | const static int sMaxCallStackDepth = 10; |
---|
| 105 | const std::string mMessage; |
---|
| 106 | const std::string mComponent; |
---|
| 107 | const std::string mLocation; |
---|
| 108 | int mCalls; |
---|
| 109 | void* mCallStack[sMaxCallStackDepth]; |
---|
| 110 | const Exception* mReason; |
---|
| 111 | |
---|
| 112 | // NOT IMPLEMENTED |
---|
| 113 | std::exception& operator=(const std::exception& right) throw (); |
---|
| 114 | }; |
---|
| 115 | |
---|
| 116 | class IOException: public Exception { |
---|
| 117 | public: |
---|
| 118 | IOException(const std::string& message, |
---|
| 119 | const std::string& component="", |
---|
| 120 | const std::string& location="", |
---|
| 121 | const Exception* reason = NULL); |
---|
| 122 | |
---|
| 123 | virtual IOException* clone() const; |
---|
| 124 | virtual const char* getTypename() const; |
---|
| 125 | |
---|
| 126 | }; |
---|
| 127 | |
---|
| 128 | } |
---|
| 129 | #endif |
---|