ctkRuntimeException.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 "ctkRuntimeException.h"
  16. //----------------------------------------------------------------------------
  17. ctkRuntimeException::ctkRuntimeException(const QString& msg, const std::exception* cause)
  18. : std::runtime_error(msg.toStdString())
  19. {
  20. if (cause)
  21. {
  22. this->cause = QString(cause->what());
  23. }
  24. }
  25. //----------------------------------------------------------------------------
  26. ctkRuntimeException::ctkRuntimeException(const ctkRuntimeException& o)
  27. : std::runtime_error(o.what()), cause(o.cause)
  28. {
  29. }
  30. //----------------------------------------------------------------------------
  31. ctkRuntimeException& ctkRuntimeException::operator=(const ctkRuntimeException& o)
  32. {
  33. std::runtime_error::operator=(o);
  34. cause = o.cause;
  35. return *this;
  36. }
  37. //----------------------------------------------------------------------------
  38. QString ctkRuntimeException::getCause() const
  39. {
  40. return cause;
  41. }
  42. //----------------------------------------------------------------------------
  43. void ctkRuntimeException::setCause(const QString& cause) throw(std::logic_error)
  44. {
  45. if (!this->cause.isEmpty()) throw std::logic_error("The cause for this ctkServiceException instance is already set");
  46. this->cause = cause;
  47. }
  48. //----------------------------------------------------------------------------
  49. const char* ctkRuntimeException::what() const throw()
  50. {
  51. static std::string fullMsg;
  52. fullMsg = std::string(std::runtime_error::what());
  53. QString causeMsg = getCause();
  54. if (!causeMsg.isEmpty()) fullMsg += std::string("\n Caused by: ") + causeMsg.toStdString();
  55. return fullMsg.c_str();
  56. }