N
Nick Keighley
Hi,
I'm trying to derive my own exceptions from runtime_error.
I want the what() to produce something like
runtime error: my error subtype A
So I tried:
class MyError: public std::runtime_error
{
public:
MyError (const std::string& what):
runtime_error(std::string("my error ") + what)
{}
};
and throw like this:
throw MyError("subtype A");
I'm sure I got the above to compile. Now it won't and the more
I look at it the less happy I am. runtime_error doesn't take a
std::string
So is there a trick to building the string in the CTOR or should I
leave building
the string to my own what(). I can't get that to work either
class MyError: public std::runtime_error
{
public:
MyError(const std::string& what):
runtime_error("my error"), what_(what)
{}
const char* what()
{
static char buffer[1024];
sprintf (buffer, "%s %s", std::runtime_error::what(),
what_);
return buffer;
}
private:
std::string what_;
};
(yes, I know a static buffer and a sprintf() call are asking for
trouble)
the compiler objects to the runtime_error::what() call
illegal call of non-static member function
well I know its non-static, but why is it illegal?
I'm trying to derive my own exceptions from runtime_error.
I want the what() to produce something like
runtime error: my error subtype A
So I tried:
class MyError: public std::runtime_error
{
public:
MyError (const std::string& what):
runtime_error(std::string("my error ") + what)
{}
};
and throw like this:
throw MyError("subtype A");
I'm sure I got the above to compile. Now it won't and the more
I look at it the less happy I am. runtime_error doesn't take a
std::string
So is there a trick to building the string in the CTOR or should I
leave building
the string to my own what(). I can't get that to work either
class MyError: public std::runtime_error
{
public:
MyError(const std::string& what):
runtime_error("my error"), what_(what)
{}
const char* what()
{
static char buffer[1024];
sprintf (buffer, "%s %s", std::runtime_error::what(),
what_);
return buffer;
}
private:
std::string what_;
};
(yes, I know a static buffer and a sprintf() call are asking for
trouble)
the compiler objects to the runtime_error::what() call
illegal call of non-static member function
well I know its non-static, but why is it illegal?