From e500d0f31ad5ebde0e428e4ae77bff55fa8c870a Mon Sep 17 00:00:00 2001 From: Guenter Obiltschnig Date: Fri, 19 Sep 2014 08:16:47 +0200 Subject: [PATCH] added poco_unexpected() macro invoking Bugcheck::unexpected() to deal with unexpected exceptions in destructors --- Foundation/include/Poco/Bugcheck.h | 14 ++++++++++++-- Foundation/src/Bugcheck.cpp | 31 ++++++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/Foundation/include/Poco/Bugcheck.h b/Foundation/include/Poco/Bugcheck.h index ecde82f2c..f48ffb27c 100644 --- a/Foundation/include/Poco/Bugcheck.h +++ b/Foundation/include/Poco/Bugcheck.h @@ -33,8 +33,8 @@ namespace Poco { class Foundation_API Bugcheck /// This class provides some static methods that are /// used by the - /// poco_assert_dbg(), poco_assert(), poco_check_ptr() - /// and poco_bugcheck() macros. + /// poco_assert_dbg(), poco_assert(), poco_check_ptr(), + /// poco_bugcheck() and poco_unexpected() macros. /// You should not invoke these methods /// directly. Use the macros instead, as they /// automatically provide useful context information. @@ -56,6 +56,12 @@ public: /// An internal error was encountered. Break into the debugger, if /// possible, then throw an BugcheckException. + static void unexpected(const char* file, int line); + /// An exception was caught in a destructor. Break into debugger, + /// if possible and report exception. Must only be called from + /// within a catch () block as it rethrows the exception to + /// determine its class. + static void debugger(const char* file, int line); /// An internal error was encountered. Break into the debugger, if /// possible. @@ -97,6 +103,10 @@ protected: #define poco_bugcheck_msg(msg) \ Poco::Bugcheck::bugcheck(msg, __FILE__, __LINE__) + + +#define poco_unexpected() \ + Poco::Bugcheck::unexpected(__FILE__, __LINE__); #define poco_debugger() \ diff --git a/Foundation/src/Bugcheck.cpp b/Foundation/src/Bugcheck.cpp index b5128067a..375d693fc 100644 --- a/Foundation/src/Bugcheck.cpp +++ b/Foundation/src/Bugcheck.cpp @@ -57,6 +57,35 @@ void Bugcheck::bugcheck(const char* msg, const char* file, int line) } +void Bugcheck::unexpected(const char* file, int line) +{ + try + { + std::string msg("Caught exception in destructor: "); + try + { + throw; + } + catch (Poco::Exception& exc) + { + msg += exc.displayText(); + } + catch (std::exception& exc) + { + msg += exc.what(); + } + catch (...) + { + msg += "unknown exception"; + } + Debugger::enter(msg, file, line); + } + catch (...) + { + } +} + + void Bugcheck::debugger(const char* file, int line) { Debugger::enter(file, line); @@ -78,6 +107,4 @@ std::string Bugcheck::what(const char* msg, const char* file, int line) } - - } // namespace Poco