BsException.h 6.3 KB

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