BsException.h 5.3 KB

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