Oracle® Fusion Middleware C++ API Reference for Oracle Coherence
12c (12.2.1.3.0)

E80355-01

coherence/lang/Exception.hpp

00001 /*
00002 * Exception.hpp
00003 *
00004 * Copyright (c) 2000, 2017, Oracle and/or its affiliates. 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/FinalView.hpp"
00023 #include "coherence/lang/ObjectArray.hpp"
00024 #include "coherence/lang/String.hpp"
00025 #include "coherence/lang/TypedHandle.hpp"
00026 #include "coherence/lang/TypedHolder.hpp"
00027 
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 derivative.
00039 * Unlike standard C++ exceptions, managed exceptions may be caught by value,
00040 * i.e. not using a const& and still be safely re-thrown
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 re-thrown 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); // re-throw managed exception
00064 *   }
00065 * catch (const std::exception& e)
00066 *   {
00067 *   std::cerr << e.what() << std::endl;
00068 *   throw; // re-throw 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 veCause  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         * Copy constructor
00101         */
00102         Exception(const Exception&);
00103 
00104 
00105     // ----- Exception interface --------------------------------------------
00106 
00107     public:
00108         /**
00109         * Return the name of the exception.
00110         */
00111         virtual String::View getName() const;
00112 
00113         /**
00114         * Returns a human-readable description of the Exception.
00115         *
00116         * Note: The String returned is held for the lifetime of this exception
00117         * to guarantee that the result does not go out of scope. This
00118         * method is used by Exception Handles to support the
00119         * std::exception::what() method.
00120         *
00121         * @return the Exception's description
00122         */
00123         virtual String::View getDescription() const;
00124 
00125         /**
00126         * Set the message associated with this exception.
00127         *
00128         * @param vsMsg  the message to set for this exception
00129         */
00130         virtual void setMessage(String::View vsMsg);
00131 
00132         /**
00133         * Return the message associated with this exception.
00134         *
00135         * @return the message associated with this exception
00136         */
00137         virtual String::View getMessage() const;
00138 
00139         /**
00140         * Return the underlying cause associated with this exception.
00141         * The underlying cause is the exception that caused this exception
00142         * to be thrown.
00143         *
00144         * @return the underlying cause associated with this exception;
00145         *         might be <tt>NULL</tt>
00146         */
00147         virtual Exception::View getCause() const;
00148 
00149         /**
00150         * Set the name of the thread on which this exception was thrown.
00151         *
00152         * @param vsThreadName  the thread name
00153         */
00154         virtual void setThreadName(String::View vsThreadName);
00155 
00156         /**
00157         * Return the name of the thread on which the exception was thrown.
00158         *
00159         * @return the name of the thread on which the exception was thrown.
00160         */
00161         virtual String::View getThreadName() const;
00162 
00163         /**
00164         * Set point at which the exception occurred.
00165         *
00166         * @param vaFrames    an array of StackTraceElements
00167         */
00168         virtual void setStackTrace(ObjectArray::View vaFrames);
00169 
00170         /**
00171         * Return the stack trace for the exception.
00172         *
00173         * @return an array of StackTraceElements.
00174         */
00175         virtual ObjectArray::View getStackTrace() const;
00176 
00177         /**
00178         * Fills the execution stack trace based on the current threads stack
00179         * and the supplied information about the current stack frame.
00180         *
00181         * @param vsFile      the file portion of the first stack frame
00182         * @param nLine       the lone portion of the first stack frame
00183         * @param vsFunction  the function portion of the first stack frame
00184         */
00185         virtual Exception::Handle fillInStackTrace(
00186                 String::View vsFile = String::null_string, int32_t nLine = 0,
00187                 String::View vsFunction = String::null_string);
00188 
00189         /**
00190         * Format the stack trace for printing
00191         *
00192         * @return the stack trace
00193         */
00194         virtual String::View formatStackTrace() const;
00195 
00196         /**
00197         * (Re)throw the exception.
00198         *
00199         * The resulting thrown exception may be caught either by it's View
00200         * type, or its alias type.
00201         */
00202         virtual void raise() const;
00203 
00204 
00205     // ----- Object interface -----------------------------------------------
00206 
00207     public:
00208         /**
00209         * {@inheritDoc}
00210         */
00211         virtual TypedHandle<const String> toString() const;
00212 
00213 
00214     // ----- data members ---------------------------------------------------
00215 
00216     protected:
00217         /**
00218         * The message associated with this exception.
00219         */
00220         FinalView<String> f_vsMessage;
00221 
00222         /**
00223         * The stack at the point the exception was thrown
00224         */
00225         FinalView<ObjectArray> f_vaStackFrames;
00226 
00227         /**
00228         * The name of the thread on which the Exception was thrown;
00229         */
00230         FinalView<String> f_vsThreadName;
00231 
00232         /**
00233         * The cause of the exception
00234         */
00235         FinalView<Exception> f_veCause;
00236 
00237         /**
00238         * The detailed human readable description of this exception.
00239         */
00240         mutable FinalView<String> f_vsDescription;
00241     };
00242 
00243 
00244 // ----- helper macros ------------------------------------------------------
00245 
00246 /**
00247 * @hideinitializer
00248 *
00249 * [re]throw a managed exception.  If the exception is referenced via a Handle
00250 * it's stack trace will be set to the current stack location, otherwise the
00251 * stack related information will unchanged.
00252 *
00253 * @param E  the exception to throw
00254 *
00255 * Usage example:
00256 * @code
00257 * COH_THROW(IOException::create("File Not Found"));
00258 * @endcode
00259 */
00260 #define COH_THROW(E) COH_NO_RETURN_STMT( \
00261     coherence::lang::coh_throw((E), __FILE__, __LINE__, COH_CURRENT_FUNCTION))
00262 
00263 /**
00264 * This macro will take the set of streamable contents and convert them to a
00265 * string which will represent the message of the exception being thrown.
00266 *
00267 * @param E         the name of the exception that will be thrown
00268 * @param CONTENTS  the streamable contents of the message to use when creating
00269 *                  the exception above.
00270 *
00271 * Note: This Macro only supports Exceptions that have a constructor that takes
00272 * a single message parameter.
00273 * Usage example:
00274 * @code
00275 * COH_THROW_STREAM(IOException, "File: " << hsFile << " not found.");
00276 * @endcode
00277 * @see COH_THROW
00278 */
00279 #define COH_THROW_STREAM(E, CONTENTS) \
00280     COH_THROW (E::create(String::View(COH_TO_STRING(CONTENTS))));
00281 
00282 // ----- free functions -----------------------------------------------------
00283 
00284 /**
00285 * This helper method is used by COH_THROW to raise the exception, supplying
00286 * the stack at which it was called.  If the exception is referenced via a
00287 * handle the exception's stack will be set prior to being throw.  The function
00288 * is marked with compiler specific (no return) statements, so that any calls
00289 * to it will suppress the corresponding compiler warnings.
00290 *
00291 * @param ohE         the instance of an exception object to throw
00292 * @param vsFile      the current file
00293 * @param nLine       the current line
00294 * @param vsFunction  the current function
00295 */
00296 COH_EXPORT COH_NO_RETURN_PRE void coh_throw(Exception::Holder ohE,
00297         String::View vsFile, int32_t nLine, String::View vsFunction)
00298             COH_NO_RETURN_POST;
00299 
00300 COH_CLOSE_NAMESPACE2
00301 
00302 #endif // COH_EXCEPTION_HPP
Copyright © 2000, 2017, Oracle and/or its affiliates. All rights reserved.