00001 /* 00002 * Exception.hpp 00003 * 00004 * Copyright 2001-2008 by Oracle. All rights reserved. 00005 * 00006 * Oracle is a registered trademarks of Oracle Corporation and/or its 00007 * affiliates. 00008 * 00009 * This software is the confidential and proprietary information of Oracle 00010 * Corporation. You shall not disclose such confidential and proprietary 00011 * information and shall use it only in accordance with the terms of the 00012 * license agreement you entered into with Oracle. 00013 * 00014 * This notice may not be removed or altered. 00015 */ 00016 #ifndef COH_EXCEPTION_HPP 00017 #define COH_EXCEPTION_HPP 00018 00019 #include "coherence/lang/compatibility.hpp" 00020 00021 #include "coherence/lang/throwable_spec.hpp" 00022 #include "coherence/lang/ObjectArray.hpp" 00023 #include "coherence/lang/String.hpp" 00024 #include "coherence/lang/TypedHandle.hpp" 00025 #include "coherence/lang/TypedHolder.hpp" 00026 00027 #include <iostream> 00028 #include <stdexcept> 00029 00030 COH_OPEN_NAMESPACE2(coherence,lang) 00031 00032 00033 /** 00034 * Base class for all exceptions used in Coherence. 00035 * 00036 * Exceptions are not thrown directly, but rather via the COH_THROW macro. The 00037 * macro will record the stack trace and throw the managed Exception such that 00038 * it may be caught either via its View type, or as a std::exception derivitive. 00039 * Unlike standard C++ exceptions, managed exceptions may be caught by value, 00040 * i.e. not using a const& and still be safely rethrown 00041 * (via the COH_THROW macro) without risking object slicing. This allows caught 00042 * exceptions to be stored as data member or local variables outside of a 00043 * try/catch block and to be safely rethrown at a later time. 00044 * 00045 * New exception classes are declared using the throwable_spec<> helper template. 00046 * 00047 * @code 00048 * try 00049 * { 00050 * ... 00051 * COH_THROW (IOException::create("some error")); 00052 * ... 00053 * } 00054 * catch (IOException::View vIoe) 00055 * { 00056 * std::cerr << vIoe << std::endl; 00057 * ... 00058 * } 00059 * catch (Exception::View vEx) 00060 * { 00061 * std::cerr << vEx << std::endl; 00062 * ... 00063 * COH_THROW (vEx); // rethrow managed exception 00064 * } 00065 * catch (const std::exception& e) 00066 * { 00067 * std::cerr << e.what() << std::endl; 00068 * throw; // rethrow standard exception 00069 * } 00070 * @endcode 00071 * 00072 * @see throwable 00073 * 00074 * @author mf 2007.05.05 00075 */ 00076 class COH_EXPORT Exception 00077 : public throwable_spec<Exception, 00078 extends<Object, std::exception>, 00079 implements<>, 00080 Object::View> 00081 { 00082 friend class factory<Exception>; 00083 00084 // ----- constructors --------------------------------------------------- 00085 00086 protected: 00087 /** 00088 * Create a new Exception object 00089 * 00090 * @param vsMsg the message of the exception 00091 * @param vCause the underlying cause of the exception; 00092 * can be <tt>NULL</tt> 00093 * 00094 * @return a Handle to the created Exception 00095 */ 00096 Exception(String::View vsMsg = String::NULL_STRING, 00097 Exception::View veCause = NULL); 00098 00099 00100 // ----- Exception interface -------------------------------------------- 00101 00102 public: 00103 /** 00104 * Return the name of the exception. 00105 */ 00106 virtual String::View getName() const; 00107 00108 /** 00109 * Returns a human-readable description of the Exception. 00110 * 00111 * Note: The String returned is held for the lifetime of this exception 00112 * to guarantee that the result does not go out of scope. This 00113 * method is used by Exception Handles to support the 00114 * std::exception::what() method. 00115 * 00116 * @return the Exception's description 00117 */ 00118 virtual String::View getDescription() const; 00119 00120 /** 00121 * Set the message associated with this exception. 00122 * 00123 * @param vsMsg the message to set for this exception 00124 */ 00125 virtual void setMessage(String::View vsMsg); 00126 00127 /** 00128 * Return the message associated with this exception. 00129 * 00130 * @return the message associated with this exception 00131 */ 00132 virtual String::View getMessage() const; 00133 00134 /** 00135 * Return the underlying cause associated with this exception. 00136 * The underlying cause is the exception that caused this exception 00137 * to be thrown. 00138 * 00139 * @return the underlying cause associated with this exception; 00140 * might be <tt>NULL</tt> 00141 */ 00142 virtual Exception::View getCause() const; 00143 00144 /** 00145 * Set the name of the thread on which this exception was thrown. 00146 * 00147 * @param vsThreadName the thread name 00148 */ 00149 virtual void setThreadName(String::View vsThreadName); 00150 00151 /** 00152 * Return the name of the thread on which the exception was thrown. 00153 * 00154 * @return the name of the thread on which the excetption was thrown. 00155 */ 00156 virtual String::View getThreadName() const; 00157 00158 /** 00159 * Set point at which the exception occured. 00160 * 00161 * @param vaFrames an array of StackTraceElements 00162 */ 00163 virtual void setStackTrace(ObjectArray::View vaFrames); 00164 00165 /** 00166 * Return the stack trace for the exception. 00167 * 00168 * @return an array of StackTraceElements. 00169 */ 00170 virtual ObjectArray::View getStackTrace() const; 00171 00172 /** 00173 * Fills the execution stack trace based on the current threads stack 00174 * and the supplied information about the current stack frame. 00175 * 00176 * @param vsFile the file portion of the first stack frame 00177 * @param nLine the lone portion of the first stack frame 00178 * @param vsFunction the function portion of the first stack frame 00179 */ 00180 virtual Exception::Handle fillInStackTrace( 00181 String::View vsFile = String::NULL_STRING, int32_t nLine = 0, 00182 String::View vsFunction = String::NULL_STRING); 00183 00184 /** 00185 * Print the stack trace to the suupplied stream. 00186 * 00187 * @param out the stream to print the trace to 00188 */ 00189 virtual void printStackTrace(std::ostream& out = std::cerr) const; 00190 00191 /** 00192 * (Re)throw the exception. 00193 * 00194 * The resulting thrown exception may be caught either by it's View 00195 * type, or its alias type. 00196 */ 00197 virtual void raise() const; 00198 00199 00200 // ----- Object interface ----------------------------------------------- 00201 00202 public: 00203 /** 00204 * {@inheritDoc} 00205 */ 00206 virtual void toStream(std::ostream& out) const; 00207 00208 00209 // ----- data members --------------------------------------------------- 00210 00211 protected: 00212 /** 00213 * The message associated with this exception. 00214 */ 00215 String::View m_vsMessage; 00216 00217 /** 00218 * The stack at the point the exception was thrown 00219 */ 00220 ObjectArray::View m_vaStackFrames; 00221 00222 /** 00223 * The name of the thread on which the Exception was thrown; 00224 */ 00225 String::View m_vsThreadName; 00226 00227 /** 00228 * The cause of the exception 00229 */ 00230 Exception::View m_veCause; 00231 00232 /** 00233 * The detailed human readable description of this exception. 00234 */ 00235 mutable String::View m_vsDescription; 00236 }; 00237 00238 00239 // ----- helper macros ------------------------------------------------------ 00240 00241 /** 00242 * @hideinitializer 00243 * 00244 * [re]throw a managed exception. If the exception is referenced via a Handle 00245 * it's stack trace will be set to the current stack location, otherwise the 00246 * stack related information will unchanged. 00247 * 00248 * @param E the exception to throw 00249 * 00250 * Usage example: 00251 * @code 00252 * COH_THROW(IOException::create("File Not Found")); 00253 * @endcode 00254 */ 00255 #define COH_THROW(E) COH_NO_RETURN_STMT( \ 00256 coherence::lang::coh_throw((E), __FILE__, __LINE__, COH_CURRENT_FUNCTION)) 00257 00258 /** 00259 * This macro will take the set of streamable contents and convert them to a 00260 * string which will represent the message of the exception being thrown. 00261 * 00262 * @param E the name of the exception that will be thrown 00263 * @param CONTENTS the streamable contents of the message to use when creating 00264 * the exception above. 00265 * 00266 * Note: This Macro only supports Exceptions that have a consturctor that takes 00267 * a single message parameter. 00268 * Usage example: 00269 * @code 00270 * COH_THROW_STREAM(IOException, "File: " << hsFile << " not found."); 00271 * @endcode 00272 * @see COH_THROW 00273 */ 00274 #define COH_THROW_STREAM(E, CONTENTS) \ 00275 COH_THROW (E::create(String::View(COH_TO_STRING(CONTENTS)))); 00276 00277 // ----- free functions ----------------------------------------------------- 00278 00279 /** 00280 * This helper method is used by COH_THROW to raise the exception, supplying 00281 * the stack at which it was called. If the exception is referenced via a 00282 * handle the exception's stack will be set prior to being throw. The function 00283 * is marked with compiler specific (no return) statements, so that any calls 00284 * to it will suppress the corresponding compiler warnings. 00285 * 00286 * @param ohE the instance of an exception object to throw 00287 * @param vsFile the current file 00288 * @param nLine the current line 00289 * @param vsFunction the current function 00290 */ 00291 COH_EXPORT COH_NO_RETURN_PRE void coh_throw(Exception::Holder ohE, 00292 String::View vsFile, int32_t nLine, String::View vsFunction) 00293 COH_NO_RETURN_POST; 00294 00295 COH_CLOSE_NAMESPACE2 00296 00297 #endif // COH_EXCEPTION_HPP