include/EDoc/exceptions.h

Go to the documentation of this file.
00001 /*******************************************************************************
00002 
00003    Copyright (C) 2007 by Brendon Costa
00004 
00005    This library is free software; you can redistribute it and/or modify 
00006    it under the terms of the "LGPL Like" License defined in the file COPYING 
00007    that should have been distributed along with this source.
00008 
00009    This library is distributed in the hope that it will be useful, 
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of 
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
00012 
00013    You should have received a copy of the "LGPL Like" License 
00014    along with this library; see the file COPYING. if not, it can be 
00015    obtained from the EDoc++ website: 
00016    http://edoc.sourceforge.net/license.html
00017 
00018 *******************************************************************************/
00019 #ifndef EDOC_EXCEPTIONS_H
00020 #define EDOC_EXCEPTIONS_H
00021 
00022 /** \file EDoc/exceptions.h This file contains definitions for all exception 
00023  * objects that may be thrown by objects in the EDoc namespace.
00024  */
00025 
00026 
00027 #include <string>
00028 #include <sstream>
00029 #include <exception>
00030 
00031 #include "EDoc/Logger.h"
00032 
00033 //==============================================================================
00034 /** \brief Gathers the current file name and line number to give to an exception
00035  * object upon construction.
00036  */
00037 #define EDOC_THROW_DATA __FILE__, __LINE__
00038 
00039 
00040 //==============================================================================
00041 /** \brief Use this macro to simplify throwing of all exceptions.
00042  *
00043  * This will gather source information like file name and line number, along
00044  * with allowing the user to specify additional debug information to a string
00045  * stream when throwing an exception.
00046  *
00047  * It is used like:
00048  * \code
00049  *    ...
00050  *    EDOC_THROW_EXCEPTION(MergeException, strerror(errno), 
00051  *       "More detailed developer message. Data: " << data);
00052  * \endcode
00053  */
00054 #define EDOC_THROW_EXCEPTION(ExceptionType, UserMessage, DevMessage) \
00055 { \
00056    std::ostringstream stream_kjhgbvk791; \
00057    std::ostringstream stream_kjhgbvk792; \
00058    stream_kjhgbvk791 << UserMessage; \
00059    stream_kjhgbvk792 << DevMessage; \
00060    throw ExceptionType (EDOC_THROW_DATA, stream_kjhgbvk791.str(), \
00061       stream_kjhgbvk792.str()); \
00062 }
00063 
00064 //==============================================================================
00065 /** \brief An assertion statement that will throw an exception of type
00066  * BugException if the assertion fails.
00067  *
00068  * The BugException is not supposed to be caught except in the main() function.
00069  */
00070 #define EDOC_ASSERT(Condition, Message) \
00071 { \
00072    if (!(Condition)) \
00073    { \
00074       EDOC_Fatal("Assertion failure. \"" << #Condition << "\"" \
00075          << " : " << Message); \
00076       EDOC_THROW_EXCEPTION(::EDoc::BugException, "Assertion failure.", \
00077          #Condition << " : " << Message); \
00078    } \
00079    else \
00080    { \
00081       EDOC_Finest("Assertion success. \"" << #Condition << "\""); \
00082    } \
00083 }
00084 
00085 
00086 //==============================================================================
00087 /** \brief Macro defined to simply create a new exception type.
00088  *
00089  * This can be used for creating simple exception types that have the standard
00090  * functionality of the EDoc::Throwable type. 
00091  *
00092  * \param NewExceptionType The name of the new exception type to create.
00093  *
00094  * \param BaseExceptionType The name of the exception type that the new one will
00095  * derive from.
00096  */
00097 #define EDOC_NEW_EXCEPTION(NewExceptionType, BaseExceptionType) \
00098    class NewExceptionType : public BaseExceptionType \
00099    { \
00100    public: \
00101       NewExceptionType(const char* type_name_in,\
00102             const char* file_in, \
00103             int line_in,  \
00104             std::string user_message_in = "", \
00105             std::string dev_message_in = "") : \
00106          \
00107          BaseExceptionType(type_name_in, file_in, line_in, user_message_in, \
00108             dev_message_in) \
00109       { \
00110       } \
00111       \
00112       NewExceptionType(const char* file_in, \
00113             int line_in,  \
00114             std::string user_message_in = "", \
00115             std::string dev_message_in = "") : \
00116          \
00117          BaseExceptionType(#NewExceptionType, file_in, line_in, \
00118             user_message_in, dev_message_in) \
00119       { \
00120       } \
00121    };   
00122 
00123 namespace EDoc
00124 {
00125    //===========================================================================
00126    /** \brief Base class from which all EDoc exception types will derive.
00127     *
00128     * This provides some simple functionality for useful exception objects.
00129     */
00130    class Throwable : public std::exception
00131    {
00132    public:
00133 
00134       //------------------------------------------------------------------------
00135       /** \brief Expected to be created in a throw statement.
00136        *
00137        * This provides a good representation of an error that provides enough
00138        * information that can be used to help debug problems and also to provide
00139        * succint useful information to a user when an error occurs.
00140        *
00141        * \param type_name_in This is the name of the exception type being
00142        * thrown.
00143        *
00144        * \param file_in The name of the source file from where the exception is
00145        * being thrown.
00146        *
00147        * \param line_in The line of the source file from where the exception is
00148        * being thrown.
00149        *
00150        * \param user_message_in A string that should be displayed to a user
00151        * describing succintly the error that occured.
00152        *
00153        * \param dev_message_in A more detailed message describing a bit of the
00154        * state information that may have caused the error to occur.
00155        */
00156       Throwable(const char* type_name_in,
00157             const char* file_in, 
00158             int line_in, 
00159             std::string user_message_in = "",
00160             std::string dev_message_in = "");
00161 
00162       //------------------------------------------------------------------------
00163       /** \brief Destructor.
00164        */
00165       virtual ~Throwable() throw() {}
00166 
00167       //------------------------------------------------------------------------
00168       /** \brief Return a string of information so that this can be used as a 
00169        * std::exception instance.
00170        */
00171       virtual const char* what() const throw();
00172 
00173       //------------------------------------------------------------------------
00174       /** \brief Returns the name of the type of exception that was thrown.
00175        */
00176       std::string GetType() const;
00177       
00178       //------------------------------------------------------------------------
00179       /** \brief Returns the name of the source file from where the exception
00180        * was thrown.
00181        */
00182       std::string GetFile() const;
00183 
00184       //------------------------------------------------------------------------
00185       /** \brief Returns the line number of the source file from where the
00186        * exception was thrown.
00187        */
00188       int GetLine() const;
00189 
00190       //------------------------------------------------------------------------
00191       /** \brief Returns the succinct message describing the error for the user.
00192        */
00193       std::string GetUserMessage() const;
00194 
00195       //------------------------------------------------------------------------
00196       /** \brief Returns the more detailed message describing the error for a
00197        * developer.
00198        */
00199       std::string GetDevMessage() const;
00200 
00201       //------------------------------------------------------------------------
00202 
00203    private:
00204 
00205       //------------------------------------------------------------------------
00206       /** \brief See GetType()
00207        */
00208       std::string type_name;
00209 
00210       //------------------------------------------------------------------------
00211       /** \brief See GetFile()
00212        */
00213       std::string file;
00214 
00215       //------------------------------------------------------------------------
00216       /** \brief See GetLine()
00217        */
00218       int line;
00219 
00220       //------------------------------------------------------------------------
00221       /** \brief See GetUserMessage()
00222        */
00223       std::string user_message;
00224 
00225       //------------------------------------------------------------------------
00226       /** \brief See GetDevMessage()
00227        */
00228       std::string dev_message;
00229 
00230       //------------------------------------------------------------------------
00231       /** \brief A string that contains all the information for this exception
00232        * in a single string. This is used by the what() function.
00233        *
00234        * NOTE: It is not a good format to display to the user.
00235        */
00236       std::string detailed_message;
00237 
00238       //------------------------------------------------------------------------
00239    };
00240    //===========================================================================
00241 
00242 
00243 //==============================================================================
00244 // Define a number of Exception types for use in EDoc.
00245 //==============================================================================
00246 EDOC_NEW_EXCEPTION(BugException, Throwable)
00247 EDOC_NEW_EXCEPTION(BaseException, Throwable)
00248 EDOC_NEW_EXCEPTION(MergeException, BaseException)
00249 EDOC_NEW_EXCEPTION(PythonException, BaseException)
00250 EDOC_NEW_EXCEPTION(FileIOException, BaseException)
00251 EDOC_NEW_EXCEPTION(EOFException, FileIOException)
00252 EDOC_NEW_EXCEPTION(UnknownFileTypeException, FileIOException)
00253 
00254 }
00255 
00256 #endif // EDOC_EXCEPTIONS_H

Generated on Tue Jan 20 18:26:07 2009 for EDoc-0.2.1 by  doxygen 1.5.1