Exception base class
Hi,
I want to construct an exception base class that is easy to use.
Ideally I want to be able to throw exceptions in one liners like this:
throw Exception("string1" + "string2 <number>", 5.0);
I can use boost::format but don't want to use any boost in my code.
Would look like this.. a two liner.
string err = str( boost::format("test %s%") % "some more into");
throw Exception("string1" + "string2 <number>", 5.0);
I came up with this approach but it is not ideal.
Exception class:
class Base : public std::exception
{
public:
Base(const std::string& whatStr) : m_whatStr(whatStr) {}
virtual const char* what() const throw() { return
m_whatStr.c_str(); }
virtual ~Base() throw () {}
private:
std::string m_whatStr;
};
class TypeException : public Base
{
public:
TypeException(std::stringstream& ss) : Base1(std::string("Type
exception: " + ss.str())) {}
};
usage:
try
{
int i = 5;
double d = 10.;
std::stringstream ss;
ss << "test" << " some more into " << i << " " << d;
throw TypeException(ss);
throw TypeException("test" << " some more into " << i << " " << d);
}
catch(Base1 base)
{
std::cout << base.what() << std::endl;
}
Lars
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]