Exception.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // Exception.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/Exception.cpp#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Core
  8. // Module: Exception
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/Exception.h"
  16. #include <typeinfo>
  17. namespace Poco {
  18. Exception::Exception(int code): _pNested(0), _code(code)
  19. {
  20. }
  21. Exception::Exception(const std::string& msg, int code): _msg(msg), _pNested(0), _code(code)
  22. {
  23. }
  24. Exception::Exception(const std::string& msg, const std::string& arg, int code): _msg(msg), _pNested(0), _code(code)
  25. {
  26. if (!arg.empty())
  27. {
  28. _msg.append(": ");
  29. _msg.append(arg);
  30. }
  31. }
  32. Exception::Exception(const std::string& msg, const Exception& nested, int code): _msg(msg), _pNested(nested.clone()), _code(code)
  33. {
  34. }
  35. Exception::Exception(const Exception& exc):
  36. std::exception(exc),
  37. _msg(exc._msg),
  38. _code(exc._code)
  39. {
  40. _pNested = exc._pNested ? exc._pNested->clone() : 0;
  41. }
  42. Exception::~Exception() throw()
  43. {
  44. delete _pNested;
  45. }
  46. Exception& Exception::operator = (const Exception& exc)
  47. {
  48. if (&exc != this)
  49. {
  50. Exception* newPNested = exc._pNested ? exc._pNested->clone() : 0;
  51. delete _pNested;
  52. _msg = exc._msg;
  53. _pNested = newPNested;
  54. _code = exc._code;
  55. }
  56. return *this;
  57. }
  58. const char* Exception::name() const throw()
  59. {
  60. return "Exception";
  61. }
  62. const char* Exception::className() const throw()
  63. {
  64. return typeid(*this).name();
  65. }
  66. const char* Exception::what() const throw()
  67. {
  68. return name();
  69. }
  70. std::string Exception::displayText() const
  71. {
  72. std::string txt = name();
  73. if (!_msg.empty())
  74. {
  75. txt.append(": ");
  76. txt.append(_msg);
  77. }
  78. return txt;
  79. }
  80. void Exception::extendedMessage(const std::string& arg)
  81. {
  82. if (!arg.empty())
  83. {
  84. if (!_msg.empty()) _msg.append(": ");
  85. _msg.append(arg);
  86. }
  87. }
  88. Exception* Exception::clone() const
  89. {
  90. return new Exception(*this);
  91. }
  92. void Exception::rethrow() const
  93. {
  94. throw *this;
  95. }
  96. POCO_IMPLEMENT_EXCEPTION(LogicException, Exception, "Logic exception")
  97. POCO_IMPLEMENT_EXCEPTION(AssertionViolationException, LogicException, "Assertion violation")
  98. POCO_IMPLEMENT_EXCEPTION(NullPointerException, LogicException, "Null pointer")
  99. POCO_IMPLEMENT_EXCEPTION(NullValueException, LogicException, "Null value")
  100. POCO_IMPLEMENT_EXCEPTION(BugcheckException, LogicException, "Bugcheck")
  101. POCO_IMPLEMENT_EXCEPTION(InvalidArgumentException, LogicException, "Invalid argument")
  102. POCO_IMPLEMENT_EXCEPTION(NotImplementedException, LogicException, "Not implemented")
  103. POCO_IMPLEMENT_EXCEPTION(RangeException, LogicException, "Out of range")
  104. POCO_IMPLEMENT_EXCEPTION(IllegalStateException, LogicException, "Illegal state")
  105. POCO_IMPLEMENT_EXCEPTION(InvalidAccessException, LogicException, "Invalid access")
  106. POCO_IMPLEMENT_EXCEPTION(SignalException, LogicException, "Signal received")
  107. POCO_IMPLEMENT_EXCEPTION(UnhandledException, LogicException, "Unhandled exception")
  108. POCO_IMPLEMENT_EXCEPTION(RuntimeException, Exception, "Runtime exception")
  109. POCO_IMPLEMENT_EXCEPTION(NotFoundException, RuntimeException, "Not found")
  110. POCO_IMPLEMENT_EXCEPTION(ExistsException, RuntimeException, "Exists")
  111. POCO_IMPLEMENT_EXCEPTION(TimeoutException, RuntimeException, "Timeout")
  112. POCO_IMPLEMENT_EXCEPTION(SystemException, RuntimeException, "System exception")
  113. POCO_IMPLEMENT_EXCEPTION(RegularExpressionException, RuntimeException, "Error in regular expression")
  114. POCO_IMPLEMENT_EXCEPTION(LibraryLoadException, RuntimeException, "Cannot load library")
  115. POCO_IMPLEMENT_EXCEPTION(LibraryAlreadyLoadedException, RuntimeException, "Library already loaded")
  116. POCO_IMPLEMENT_EXCEPTION(NoThreadAvailableException, RuntimeException, "No thread available")
  117. POCO_IMPLEMENT_EXCEPTION(PropertyNotSupportedException, RuntimeException, "Property not supported")
  118. POCO_IMPLEMENT_EXCEPTION(PoolOverflowException, RuntimeException, "Pool overflow")
  119. POCO_IMPLEMENT_EXCEPTION(NoPermissionException, RuntimeException, "No permission")
  120. POCO_IMPLEMENT_EXCEPTION(OutOfMemoryException, RuntimeException, "Out of memory")
  121. POCO_IMPLEMENT_EXCEPTION(DataException, RuntimeException, "Data error")
  122. POCO_IMPLEMENT_EXCEPTION(DataFormatException, DataException, "Bad data format")
  123. POCO_IMPLEMENT_EXCEPTION(SyntaxException, DataException, "Syntax error")
  124. POCO_IMPLEMENT_EXCEPTION(CircularReferenceException, DataException, "Circular reference")
  125. POCO_IMPLEMENT_EXCEPTION(PathSyntaxException, SyntaxException, "Bad path syntax")
  126. POCO_IMPLEMENT_EXCEPTION(IOException, RuntimeException, "I/O error")
  127. POCO_IMPLEMENT_EXCEPTION(ProtocolException, IOException, "Protocol error")
  128. POCO_IMPLEMENT_EXCEPTION(FileException, IOException, "File access error")
  129. POCO_IMPLEMENT_EXCEPTION(FileExistsException, FileException, "File exists")
  130. POCO_IMPLEMENT_EXCEPTION(FileNotFoundException, FileException, "File not found")
  131. POCO_IMPLEMENT_EXCEPTION(PathNotFoundException, FileException, "Path not found")
  132. POCO_IMPLEMENT_EXCEPTION(FileReadOnlyException, FileException, "File is read-only")
  133. POCO_IMPLEMENT_EXCEPTION(FileAccessDeniedException, FileException, "Access to file denied")
  134. POCO_IMPLEMENT_EXCEPTION(CreateFileException, FileException, "Cannot create file")
  135. POCO_IMPLEMENT_EXCEPTION(OpenFileException, FileException, "Cannot open file")
  136. POCO_IMPLEMENT_EXCEPTION(WriteFileException, FileException, "Cannot write file")
  137. POCO_IMPLEMENT_EXCEPTION(ReadFileException, FileException, "Cannot read file")
  138. POCO_IMPLEMENT_EXCEPTION(UnknownURISchemeException, RuntimeException, "Unknown URI scheme")
  139. POCO_IMPLEMENT_EXCEPTION(ApplicationException, Exception, "Application exception")
  140. POCO_IMPLEMENT_EXCEPTION(BadCastException, RuntimeException, "Bad cast exception")
  141. } // namespace Poco