Changeset 3196 for code/trunk/src/util/Exception.h
- Timestamp:
- Jun 20, 2009, 9:20:47 AM (16 years ago)
- Location:
- code/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
- Property svn:mergeinfo changed
/code/branches/pch (added) merged: 3114-3118,3124-3125,3127-3131,3133,3138-3194
- Property svn:mergeinfo changed
-
code/trunk/src/util/Exception.h
r3068 r3196 30 30 @file 31 31 @brief 32 Declaration of the Exception class.32 Declaration of facilities to handle exceptions. 33 33 */ 34 34 … … 38 38 #include "UtilPrereqs.h" 39 39 40 #include <exception> 41 #include <sstream> 40 42 #include <string> 41 #include <exception>42 #include <cassert>43 43 #include "Debug.h" 44 44 45 45 namespace orxonox 46 46 { 47 /** 48 @brief 49 Base class for all exceptions (derived from std::exception). 50 @details 51 This class provides support for information about the file, the line 52 and the function the error occured. 53 */ 47 54 class _UtilExport Exception : public std::exception 48 55 { 49 56 public: 50 51 Exception(const std::string& description, int lineNumber, 57 /** 58 @brief 59 Creates the exception but doesn't yet compose the full descrption (because of the virtual functions) 60 @param description 61 Exception description as string. This message is supposed to help developers! 62 */ 63 Exception(const std::string& description, unsigned int lineNumber, 52 64 const char* filename, const char* functionName); 65 //! Simplified constructor with just a description. If you need more, use the other one. 53 66 Exception(const std::string& description); 54 67 55 // / Needed for compatibility with std::exception (from Ogre::Exception)68 //! Needed for compatibility with std::exception 56 69 virtual ~Exception() throw() { } 57 70 71 //! Returns a full description with type, line, file and function 58 72 virtual const std::string& getFullDescription() const; 73 //! Returns the string name of the exception type 59 74 virtual std::string getTypeName() const = 0; 75 //! Returns the short developer written exception 60 76 virtual const std::string& getDescription() const { return this->description_; } 61 virtual const int getLineNumber() const { return this->lineNumber_; } 77 //! Returns the line number on which the exception occurred. 78 virtual const unsigned int getLineNumber() const { return this->lineNumber_; } 79 //! Returns the function in which the exception occurred. 62 80 virtual const std::string& getFunctionName() const { return this->functionName_; } 81 //! Returns the filename in which the exception occurred. 82 virtual const std::string& getFilename() const { return this->filename_; } 63 83 64 // / Override std::exception::what (from Ogre::Exception)84 //! Returns a full description of the error. 65 85 const char* what() const throw() { return getFullDescription().c_str(); } 66 86 67 87 protected: 68 std::string description_; 69 int lineNumber_;70 std::string functionName_; 71 std::string filename_; 88 std::string description_; //!< User typed text about why the exception occurred 89 unsigned int lineNumber_; //!< Line on which the exception occurred 90 std::string functionName_; //!< Function (including namespace and class) where the exception occurred 91 std::string filename_; //!< File where the exception occurred 72 92 // mutable because "what()" is a const method 73 mutable std::string fullDescription_; 93 mutable std::string fullDescription_; //!< Full description with line, file and function 74 94 }; 75 95 76 96 //! Creates a new type of exception that inherits from tracker::Exception 77 97 #define CREATE_ORXONOX_EXCEPTION(ExceptionName) \ 78 98 class ExceptionName##Exception : public Exception \ 79 99 { \ 80 100 public: \ 81 ExceptionName##Exception(const std::string& description, int lineNumber, \ 82 const char* filename, const char* functionName) \ 83 : Exception(description, lineNumber, filename, functionName) \ 101 ExceptionName##Exception(const std::string& description, \ 102 unsigned int lineNumber, const char* filename, \ 103 const char* functionName) \ 104 : Exception(description, lineNumber, filename, functionName) \ 84 105 { } \ 85 106 \ 86 107 ExceptionName##Exception(const std::string& description) \ 87 : Exception(description)\108 : Exception(description) \ 88 109 { } \ 89 110 \ … … 91 112 \ 92 113 std::string getTypeName() const { return #ExceptionName; } \ 93 } ;114 } 94 115 95 116 // Creates all possible exception types. 96 117 // If you want to add a new type, simply copy and adjust a new line here. 118 #ifndef DOXYGEN_SHOULD_SKIP_THIS 97 119 CREATE_ORXONOX_EXCEPTION(General); 98 120 CREATE_ORXONOX_EXCEPTION(FileNotFound); … … 106 128 CREATE_ORXONOX_EXCEPTION(NoGraphics); 107 129 CREATE_ORXONOX_EXCEPTION(AbortLoading); 108 } 130 #endif 109 131 110 132 /** 111 133 @brief 112 Helper function that creates an exception, displays the message, but doesn't throw it. 134 Helper function that forwards an exception and displays the message. 135 @details 136 This is necessary because only when using 'throw' the objects storage is managed. 113 137 */ 114 138 template <class T> 115 inline const T& InternalHandleException(const T& exception)139 inline const T& exceptionThrowerHelper(const T& exception) 116 140 { 117 141 // let the catcher decide whether to display the message below level 4 … … 120 144 } 121 145 122 #define ThrowException(type, description) \ 123 throw InternalHandleException(type##Exception(description, __LINE__, __FILE__, __FUNCTIONNAME__)) 146 /** 147 @brief 148 Throws an exception and logs a message beforehand. 149 @param type 150 Type of the exception as literal (General, Initialisation, etc.) 151 @param description 152 Exception description as string 153 */ 154 #define ThrowException(type, description, ...) \ 155 throw orxonox::exceptionThrowerHelper(type##Exception(static_cast<std::ostringstream&>(std::ostringstream().flush() << description).str(), __LINE__, __FILE__, __FUNCTIONNAME__)) 124 156 125 // define an assert macro that can display a message 126 #ifndef NDEBUG 127 #define OrxAssert(Assertion, ErrorMessage) \ 128 Assertion ? ((void)0) : (void)(orxonox::OutputHandler::getOutStream().setOutputLevel(ORX_ERROR) << ErrorMessage << std::endl); \ 129 assert(Assertion) 130 #else 131 #define OrxAssert(condition, errorMessage) ((void)0) 132 #endif 157 } /* namespace orxonox */ 133 158 134 159 #endif /* _Exception_H__ */
Note: See TracChangeset
for help on using the changeset viewer.