BsException.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #pragma once
  2. #include "BsPrerequisitesUtil.h"
  3. #if defined(_MSC_VER)
  4. #undef __PRETTY_FUNCTION__
  5. #define __PRETTY_FUNCTION__ __FUNCSIG__
  6. #endif
  7. namespace BansheeEngine
  8. {
  9. /**
  10. * @brief Base class for all Banshee exceptions.
  11. */
  12. class Exception : public std::exception
  13. {
  14. public:
  15. Exception(const char* type, const String& description, const String& source)
  16. :mLine(0), mTypeName(type), mDescription(description), mSource(source)
  17. { }
  18. Exception(const char* type, const String& description, const String& source, const char* file, long line)
  19. : mLine(line), mTypeName(type), mDescription(description), mSource(source), mFile(file)
  20. { }
  21. Exception(const Exception& rhs)
  22. : mLine(rhs.mLine), mTypeName(rhs.mTypeName), mDescription(rhs.mDescription),
  23. mSource(rhs.mSource), mFile(rhs.mFile)
  24. { }
  25. ~Exception() throw() {}
  26. void operator = (const Exception& rhs)
  27. {
  28. mDescription = rhs.mDescription;
  29. mSource = rhs.mSource;
  30. mFile = rhs.mFile;
  31. mLine = rhs.mLine;
  32. mTypeName = rhs.mTypeName;
  33. }
  34. /**
  35. * @brief Returns a string with the full description of the exception.
  36. *
  37. * @note The description contains the error number, the description
  38. * supplied by the thrower, what routine threw the exception,
  39. * and will also supply extra platform-specific information
  40. * where applicable.
  41. */
  42. virtual const String& getFullDescription() const
  43. {
  44. if (mFullDesc.empty())
  45. {
  46. StringStream desc;
  47. desc << "BANSHEE EXCEPTION(" << mTypeName << "): "
  48. << mDescription
  49. << " in " << mSource;
  50. if (mLine > 0)
  51. {
  52. desc << " at " << mFile << " (line " << mLine << ")";
  53. }
  54. mFullDesc = desc.str();
  55. }
  56. return mFullDesc;
  57. }
  58. /**
  59. * @brief Gets the source function that threw the exception.
  60. */
  61. virtual const String& getSource() const { return mSource; }
  62. /**
  63. * @brief Gets the source file name in which the exception was thrown.
  64. */
  65. virtual const String& getFile() const { return mFile; }
  66. /**
  67. * @brief Gets line number on which the exception was thrown.
  68. */
  69. virtual long getLine() const { return mLine; }
  70. /**
  71. * @brief Gets a short description about the exception.
  72. */
  73. virtual const String& getDescription(void) const { return mDescription; }
  74. /**
  75. * @brief Overriden std::exception::what. Returns the same value as "getFullDescription".
  76. */
  77. const char* what() const override { return getFullDescription().c_str(); }
  78. protected:
  79. long mLine;
  80. String mTypeName;
  81. String mDescription;
  82. String mSource;
  83. String mFile;
  84. mutable String mFullDesc;
  85. };
  86. /**
  87. * @brief Exception for signaling not implemented parts of the code.
  88. */
  89. class NotImplementedException : public Exception
  90. {
  91. public:
  92. NotImplementedException(const String& inDescription, const String& inSource, const char* inFile, long inLine)
  93. : Exception("NotImplementedException", inDescription, inSource, inFile, inLine) {}
  94. };
  95. /**
  96. * @brief Exception for signaling file system errors when file could not be found.
  97. */
  98. class FileNotFoundException : public Exception
  99. {
  100. public:
  101. FileNotFoundException(const String& inDescription, const String& inSource, const char* inFile, long inLine)
  102. : Exception("FileNotFoundException", inDescription, inSource, inFile, inLine) {}
  103. };
  104. /**
  105. * @brief Exception for signaling general IO errors.
  106. *
  107. * @note An example being failed to open a file or a network connection.
  108. */
  109. class IOException : public Exception
  110. {
  111. public:
  112. IOException(const String& inDescription, const String& inSource, const char* inFile, long inLine)
  113. : Exception("IOException", inDescription, inSource, inFile, inLine) {}
  114. };
  115. /**
  116. * @brief Exception for signaling not currently executing code in not in a valid state.
  117. */
  118. class InvalidStateException : public Exception
  119. {
  120. public:
  121. InvalidStateException(const String& inDescription, const String& inSource, const char* inFile, long inLine)
  122. : Exception("InvalidStateException", inDescription, inSource, inFile, inLine) {}
  123. };
  124. /**
  125. * @brief Exception for signaling not some parameters you have provided are not valid.
  126. */
  127. class InvalidParametersException : public Exception
  128. {
  129. public:
  130. InvalidParametersException(const String& inDescription, const String& inSource, const char* inFile, long inLine)
  131. : Exception("InvalidParametersException", inDescription, inSource, inFile, inLine) {}
  132. };
  133. /**
  134. * @brief Exception for signaling an internal error, normally something that shouldn't have happened or
  135. * wasn't anticipated by the programmers of that system.
  136. */
  137. class InternalErrorException : public Exception
  138. {
  139. public:
  140. InternalErrorException(const String& inDescription, const String& inSource, const char* inFile, long inLine)
  141. : Exception("InternalErrorException", inDescription, inSource, inFile, inLine) {}
  142. };
  143. /**
  144. * @brief Exception for signaling an error in a rendering API.
  145. */
  146. class RenderingAPIException : public Exception
  147. {
  148. public:
  149. RenderingAPIException(const String& inDescription, const String& inSource, const char* inFile, long inLine)
  150. : Exception("RenderingAPIException", inDescription, inSource, inFile, inLine) {}
  151. };
  152. /**
  153. * @brief Exception for signaling an error in an unit test.
  154. */
  155. class UnitTestException : public Exception
  156. {
  157. public:
  158. UnitTestException(const String& inDescription, const String& inSource, const char* inFile, long inLine)
  159. : Exception("UnitTestException", inDescription, inSource, inFile, inLine) {}
  160. };
  161. /**
  162. * @brief Macro for throwing exceptions that will automatically fill out function name, file name and line number of the exception.
  163. */
  164. // The exception thrown at the end isn't actually ever getting executed, it is just to notify the compiler that execution
  165. // won't continue past this point (e.g. if a function needs to return a value otherwise).
  166. #ifndef BS_EXCEPT
  167. #define BS_EXCEPT(type, desc) \
  168. { \
  169. static_assert((std::is_base_of<BansheeEngine::Exception, type##>::value), "Invalid exception type (" #type ") for BS_EXCEPT macro. It needs to derive from BansheeEngine::Exception."); \
  170. gCrashHandler().reportCrash(#type, desc, __PRETTY_FUNCTION__, __FILE__, __LINE__); \
  171. PlatformUtility::terminate(true); \
  172. throw type##(desc, __PRETTY_FUNCTION__, __FILE__, __LINE__); \
  173. }
  174. #endif
  175. }