Exception.hh
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef EXCEPTION_HH_
00018 # define EXCEPTION_HH_
00019
00020 # include <string>
00021 # include <sstream>
00022
00024 class Exception
00025 {
00026 public:
00027 Exception();
00028 Exception(const std::string& msg);
00029 virtual ~Exception();
00030
00031 const std::string& what() const;
00032 friend std::ostream& operator<< (std::ostream& os, const Exception& e);
00033
00034 protected:
00035 std::string msg_;
00036 };
00037
00038
00039 inline Exception::Exception()
00040 : msg_("Undefined exception")
00041 {
00042 }
00043
00044 inline Exception::Exception(const std::string& msg)
00045 : msg_(msg)
00046 {
00047 }
00048
00049 inline Exception::~Exception()
00050 {
00051 }
00052
00053 inline const std::string& Exception::what() const
00054 {
00055 return msg_;
00056 }
00057
00058 inline std::ostream& operator<< (std::ostream& os, const Exception& e)
00059 {
00060 os << e.msg_;
00061 return os;
00062 }
00063
00064
00065 # define THROW(Excpt, Msg) \
00066 do { \
00067 std::ostringstream os; \
00068 os << Msg; \
00069 throw Excpt(os.rdbuf()->str()); \
00070 } while (0)
00071
00072 # define PRINT_AND_THROW(Excpt, Msg) \
00073 do { \
00074 std::ostringstream os; \
00075 os << Msg; \
00076 Excpt e(os.rdbuf()->str()); \
00077 ERR(e.what()); \
00078 throw e; \
00079 } while (0)
00080
00081
00082 #endif