BsException.h 6.5 KB

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