ctkException.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #include "ctkException.h"
  16. #include <typeinfo>
  17. #include <QDebug>
  18. // --------------------------------------------------------------------------
  19. ctkException::ctkException(const QString& msg)
  20. : Msg(msg), NestedException(0)
  21. {
  22. }
  23. // --------------------------------------------------------------------------
  24. ctkException::ctkException(const QString& msg, const ctkException& cause)
  25. : Msg(msg), NestedException(cause.clone())
  26. {
  27. }
  28. // --------------------------------------------------------------------------
  29. ctkException::ctkException(const ctkException& exc)
  30. : std::exception(exc), Msg(exc.Msg)
  31. {
  32. NestedException = exc.NestedException ? exc.NestedException->clone() : 0;
  33. }
  34. // --------------------------------------------------------------------------
  35. ctkException::~ctkException() throw()
  36. {
  37. delete NestedException;
  38. }
  39. // --------------------------------------------------------------------------
  40. ctkException& ctkException::operator=(const ctkException& exc)
  41. {
  42. if (&exc != this)
  43. {
  44. delete NestedException;
  45. Msg = exc.Msg;
  46. NestedException = exc.NestedException ? exc.NestedException->clone() : 0;
  47. }
  48. return *this;
  49. }
  50. // --------------------------------------------------------------------------
  51. const ctkException* ctkException::cause() const throw()
  52. {
  53. return NestedException;
  54. }
  55. // --------------------------------------------------------------------------
  56. void ctkException::setCause(const ctkException& cause)
  57. {
  58. delete NestedException;
  59. NestedException = cause.clone();
  60. }
  61. // --------------------------------------------------------------------------
  62. const char *ctkException::name() const throw()
  63. {
  64. return "ctkException";
  65. }
  66. // --------------------------------------------------------------------------
  67. const char* ctkException::className() const throw()
  68. {
  69. return typeid(*this).name();
  70. }
  71. // --------------------------------------------------------------------------
  72. const char* ctkException::what() const throw()
  73. {
  74. static std::string txt;
  75. txt = std::string(name());
  76. if (!Msg.isEmpty())
  77. {
  78. txt += ": ";
  79. txt += Msg.toStdString();
  80. }
  81. if (NestedException)
  82. {
  83. txt += std::string("\n Caused by: ") + NestedException->what();
  84. }
  85. return txt.c_str();
  86. }
  87. // --------------------------------------------------------------------------
  88. ctkException* ctkException::clone() const
  89. {
  90. return new ctkException(*this);
  91. }
  92. // --------------------------------------------------------------------------
  93. void ctkException::rethrow() const
  94. {
  95. throw *this;
  96. }
  97. //----------------------------------------------------------------------------
  98. QDebug operator<<(QDebug dbg, const ctkException& exc)
  99. {
  100. dbg << exc.what();
  101. return dbg.maybeSpace();
  102. }
  103. CTK_IMPLEMENT_EXCEPTION(ctkRuntimeException, ctkException, "ctkRuntimeException")
  104. CTK_IMPLEMENT_EXCEPTION(ctkInvalidArgumentException, ctkRuntimeException, "ctkInvalidArgumentException")
  105. CTK_IMPLEMENT_EXCEPTION(ctkIllegalStateException, ctkRuntimeException, "ctkIllegalStateException")