ErrorHandler.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // ErrorHandler.h
  3. //
  4. // $Id: //poco/1.3/Foundation/include/Poco/ErrorHandler.h#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Threading
  8. // Module: ErrorHandler
  9. //
  10. // Definition of the ErrorHandler class.
  11. //
  12. // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // Permission is hereby granted, free of charge, to any person or organization
  16. // obtaining a copy of the software and accompanying documentation covered by
  17. // this license (the "Software") to use, reproduce, display, distribute,
  18. // execute, and transmit the Software, and to prepare derivative works of the
  19. // Software, and to permit third-parties to whom the Software is furnished to
  20. // do so, all subject to the following:
  21. //
  22. // The copyright notices in the Software and this entire statement, including
  23. // the above license grant, this restriction and the following disclaimer,
  24. // must be included in all copies of the Software, in whole or in part, and
  25. // all derivative works of the Software, unless such copies or derivative
  26. // works are solely in the form of machine-executable object code generated by
  27. // a source language processor.
  28. //
  29. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  30. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  31. // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  32. // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  33. // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  34. // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  35. // DEALINGS IN THE SOFTWARE.
  36. //
  37. #ifndef Foundation_ErrorHandler_INCLUDED
  38. #define Foundation_ErrorHandler_INCLUDED
  39. #include "Poco/Foundation.h"
  40. #include "Poco/Exception.h"
  41. #include "Poco/Mutex.h"
  42. namespace Poco {
  43. class Foundation_API ErrorHandler
  44. /// This is the base class for thread error handlers.
  45. ///
  46. /// An unhandled exception that causes a thread to terminate is usually
  47. /// silently ignored, since the class library cannot do anything meaningful
  48. /// about it.
  49. ///
  50. /// The Thread class provides the possibility to register a
  51. /// global ErrorHandler that is invoked whenever a thread has
  52. /// been terminated by an unhandled exception.
  53. /// The ErrorHandler must be derived from this class and can
  54. /// provide implementations of all three handleException() overloads.
  55. ///
  56. /// The ErrorHandler is always invoked within the context of
  57. /// the offending thread.
  58. {
  59. public:
  60. ErrorHandler();
  61. /// Creates the ErrorHandler.
  62. virtual ~ErrorHandler();
  63. /// Destroys the ErrorHandler.
  64. virtual void exception(const Exception& exc);
  65. /// Called when a Poco::Exception (or a subclass)
  66. /// caused the thread to terminate.
  67. ///
  68. /// This method should not throw any exception - it would
  69. /// be silently ignored.
  70. ///
  71. /// The default implementation just breaks into the debugger.
  72. virtual void exception(const std::exception& exc);
  73. /// Called when a std::exception (or a subclass)
  74. /// caused the thread to terminate.
  75. ///
  76. /// This method should not throw any exception - it would
  77. /// be silently ignored.
  78. ///
  79. /// The default implementation just breaks into the debugger.
  80. virtual void exception();
  81. /// Called when an exception that is neither a
  82. /// Poco::Exception nor a std::exception caused
  83. /// the thread to terminate.
  84. ///
  85. /// This method should not throw any exception - it would
  86. /// be silently ignored.
  87. ///
  88. /// The default implementation just breaks into the debugger.
  89. static void handle(const Exception& exc);
  90. /// Invokes the currently registered ErrorHandler.
  91. static void handle(const std::exception& exc);
  92. /// Invokes the currently registered ErrorHandler.
  93. static void handle();
  94. /// Invokes the currently registered ErrorHandler.
  95. static ErrorHandler* set(ErrorHandler* pHandler);
  96. /// Registers the given handler as the current error handler.
  97. ///
  98. /// Returns the previously registered handler.
  99. static ErrorHandler* get();
  100. /// Returns a pointer to the currently registered
  101. /// ErrorHandler.
  102. protected:
  103. static ErrorHandler* defaultHandler();
  104. /// Returns the default ErrorHandler.
  105. private:
  106. static ErrorHandler* _pHandler;
  107. static FastMutex _mutex;
  108. };
  109. //
  110. // inlines
  111. //
  112. inline ErrorHandler* ErrorHandler::get()
  113. {
  114. return _pHandler;
  115. }
  116. } // namespace Poco
  117. #endif // Foundation_ErrorHandler_INCLUDED