#include <exception> #include <string> #include <sstream> #include <typeinfo> //custom exceptions class Exception : public std::exception { public: /// /brief Create exception object as copy of another Exception(const Exception& e) throw() : std::exception(e), what_(e.what_) { } /// /brief Assign another exception object's contents to this one Exception& operator=(const Exception& rhs) throw() { what_ = rhs.what_; return *this; } /// /brief Destroy exception object ~Exception() throw() { } /// /brief Returns explanation of why exception was thrown virtual const char* what() const throw() { return what_.c_str(); } protected: /// /brief Create exception object Exception(const char* w = "") throw() : what_(w) { } /// /brief Create exception object Exception(const std::string& w) throw() : what_(w) { } /// /brief explanation of why exception was thrown std::string what_; }; /// /brief Exception thrown when a KceMutex object fails. class MutexFailed : public Exception { public: /// /brief Create exception object explicit MutexFailed(const char* w = "lock failed") : Exception(w) { } }; 用法很简单: try { ... } catch( Exception& e ) { cout << e.what() <<endl; }