ctkExceptionTest.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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.txt
  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 <QDebug>
  17. #include <cstring>
  18. #include <iostream>
  19. //-----------------------------------------------------------------------------
  20. void exc_func1()
  21. {
  22. throw ctkException("My exception");
  23. }
  24. //-----------------------------------------------------------------------------
  25. void exc_func2()
  26. {
  27. try {
  28. exc_func1();
  29. }
  30. catch (const ctkException& exc)
  31. {
  32. throw ctkRuntimeException("runtime test error", exc);
  33. }
  34. }
  35. //-----------------------------------------------------------------------------
  36. int ctkExceptionTest(int /*argc*/, char* /*argv*/[])
  37. {
  38. try
  39. {
  40. exc_func2();
  41. }
  42. catch (const ctkException& exc)
  43. {
  44. qDebug() << exc.printStackTrace();
  45. if (std::strcmp(exc.name(), "ctkRuntimeException"))
  46. {
  47. std::cerr << "Exception name mismatch." << std::endl;
  48. return EXIT_FAILURE;
  49. }
  50. if (std::strcmp(exc.what(), "ctkRuntimeException: runtime test error"))
  51. {
  52. std::cerr << "Exception what() mismatch." << std::endl;
  53. return EXIT_FAILURE;
  54. }
  55. if(exc.message() != "runtime test error")
  56. {
  57. std::cerr << "Exception message mismatch." << std::endl;
  58. return EXIT_FAILURE;
  59. }
  60. if(std::strcmp(exc.cause()->name(), "ctkException"))
  61. {
  62. std::cerr << "Exception cause mismatch." << std::endl;
  63. return EXIT_FAILURE;
  64. }
  65. QList<QString> trace = exc.stackTrace();
  66. ctkException* clonedExc = exc.clone();
  67. if (trace != clonedExc->stackTrace())
  68. {
  69. std::cerr << "Cloned exception stack trace mismatch." << std::endl;
  70. return EXIT_FAILURE;
  71. }
  72. try
  73. {
  74. clonedExc->rethrow();
  75. }
  76. catch (const ctkRuntimeException&)
  77. {
  78. delete clonedExc;
  79. }
  80. catch (const ctkException&)
  81. {
  82. std::cerr << "Wrong exception type rethrown" << std::endl;
  83. delete clonedExc;
  84. return EXIT_FAILURE;
  85. }
  86. }
  87. return EXIT_SUCCESS;
  88. }