1 //
2 // ErrorHandler.h
3 //
4 // Library: Foundation
5 // Package: Threading
6 // Module:  ErrorHandler
7 //
8 // Definition of the ErrorHandler class.
9 //
10 // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
11 // and Contributors.
12 //
13 // SPDX-License-Identifier:	BSL-1.0
14 //
15 
16 
17 #ifndef Foundation_ErrorHandler_INCLUDED
18 #define Foundation_ErrorHandler_INCLUDED
19 
20 
21 #include "Poco/Foundation.h"
22 #include "Poco/Exception.h"
23 #include "Poco/Mutex.h"
24 
25 
26 namespace Poco {
27 
28 
29 class Foundation_API ErrorHandler
30 	/// This is the base class for thread error handlers.
31 	///
32 	/// An unhandled exception that causes a thread to terminate is usually
33 	/// silently ignored, since the class library cannot do anything meaningful
34 	/// about it.
35 	///
36 	/// The Thread class provides the possibility to register a
37 	/// global ErrorHandler that is invoked whenever a thread has
38 	/// been terminated by an unhandled exception.
39 	/// The ErrorHandler must be derived from this class and can
40 	/// provide implementations of all three exception() overloads.
41 	///
42 	/// The ErrorHandler is always invoked within the context of
43 	/// the offending thread.
44 {
45 public:
46 	ErrorHandler();
47 		/// Creates the ErrorHandler.
48 
49 	virtual ~ErrorHandler();
50 		/// Destroys the ErrorHandler.
51 
52 	virtual void exception(const Exception& exc);
53 		/// Called when a Poco::Exception (or a subclass)
54 		/// caused the thread to terminate.
55 		///
56 		/// This method should not throw any exception - it would
57 		/// be silently ignored.
58 		///
59 		/// The default implementation just breaks into the debugger.
60 
61 	virtual void exception(const std::exception& exc);
62 		/// Called when a std::exception (or a subclass)
63 		/// caused the thread to terminate.
64 		///
65 		/// This method should not throw any exception - it would
66 		/// be silently ignored.
67 		///
68 		/// The default implementation just breaks into the debugger.
69 
70 	virtual void exception();
71 		/// Called when an exception that is neither a
72 		/// Poco::Exception nor a std::exception caused
73 		/// the thread to terminate.
74 		///
75 		/// This method should not throw any exception - it would
76 		/// be silently ignored.
77 		///
78 		/// The default implementation just breaks into the debugger.
79 
80 	static void handle(const Exception& exc);
81 		/// Invokes the currently registered ErrorHandler.
82 
83 	static void handle(const std::exception& exc);
84 		/// Invokes the currently registered ErrorHandler.
85 
86 	static void handle();
87 		/// Invokes the currently registered ErrorHandler.
88 
89 	static ErrorHandler* set(ErrorHandler* pHandler);
90 		/// Registers the given handler as the current error handler.
91 		///
92 		/// Returns the previously registered handler.
93 
94 	static ErrorHandler* get();
95 		/// Returns a pointer to the currently registered
96 		/// ErrorHandler.
97 
98 protected:
99 	static ErrorHandler* defaultHandler();
100 		/// Returns the default ErrorHandler.
101 
102 private:
103 	static ErrorHandler* _pHandler;
104 	static FastMutex     _mutex;
105 };
106 
107 
108 //
109 // inlines
110 //
get()111 inline ErrorHandler* ErrorHandler::get()
112 {
113 	return _pHandler;
114 }
115 
116 
117 } // namespace Poco
118 
119 
120 #endif // Foundation_ErrorHandler_INCLUDED
121