Exception.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Exception.h"
  25. #include "Log.h"
  26. #include "StringUtils.h"
  27. #include <angelscript.h>
  28. #include "DebugNew.h"
  29. Exception::Exception(const std::string& what, bool logError) :
  30. mWhat(what)
  31. {
  32. if (logError)
  33. LOGERROR(what);
  34. }
  35. #ifdef _DEBUG
  36. Exception::Exception(const std::string& what, const char* file, int line, bool logError)
  37. {
  38. mWhat = what + std::string(" at ") + std::string(file) + std::string(", line ") + toString(line);
  39. if (logError)
  40. LOGERROR(mWhat);
  41. }
  42. #endif
  43. Exception::~Exception() throw()
  44. {
  45. }
  46. const char* Exception::what() const throw()
  47. {
  48. return mWhat.c_str();
  49. }
  50. void checkAndThrowException(const std::string& what)
  51. {
  52. asIScriptContext* context = asGetActiveContext();
  53. if (context)
  54. {
  55. static std::string lastWhat;
  56. lastWhat = what;
  57. LOGERROR(lastWhat);
  58. context->SetException(lastWhat.c_str());
  59. }
  60. else
  61. throw Exception(what);
  62. }
  63. #ifdef _DEBUG
  64. void checkAndThrowException(const std::string& what, const char* file, int line)
  65. {
  66. asIScriptContext* context = asGetActiveContext();
  67. if (context)
  68. {
  69. static std::string lastWhat;
  70. lastWhat = what + std::string(" at ") + std::string(file) + std::string(", line ") + toString(line);
  71. LOGERROR(lastWhat);
  72. context->SetException(lastWhat.c_str());
  73. }
  74. else
  75. throw Exception(what, file, line);
  76. }
  77. #endif
  78. void checkAndRethrowException(const Exception& e)
  79. {
  80. asIScriptContext* context = asGetActiveContext();
  81. if (context)
  82. {
  83. static std::string lastWhat;
  84. lastWhat = e.whatStr();
  85. context->SetException(lastWhat.c_str());
  86. }
  87. else
  88. throw e;
  89. }