BsException.h 6.3 KB

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