deriving from std::runtime_error

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?
 
K

kwikius

Nick said:
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)
{}
};

AFAICS should work OK. runtime_error has a ctor for std::string, but is
explicit though.

Probably a misleading error message somewhere and something else causing
problem ?

regards
Andy Little
 
A

acehreli

I'm trying to derive my own exceptions from runtime_error.

That's good.
I want the what() to produce something like

runtime error: my error subtype A

The "runtime error" part will not be the same on every environment.
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.

g++ compiles the code for me as well.
class MyError: public std::runtime_error
{
public:
MyError(const std::string& what):
runtime_error("my error"), what_(what)
{}

When the class has at least one member (what_) now g++ errors:

looser throw specifier for $-1òøvirtual MyError::~MyError()òù

So I added this:

~MyError() throw()
{}
const char* what()
{

That function signature is not the same as runtime_error's what(), so
it's not "overriding" but "hiding" in this case. g++ warns:

$-1òøvirtual const char* std::runtime_error::what() constòù was
hidden

And then about the same looser throw specifier. So it must be:

const char* what() const throw()

static char buffer[1024];
sprintf (buffer, "%s %s", std::runtime_error::what(),
what_);

One last warning from g++:

cannot pass objects of non-POD type $-1òøconst struct
std::stringòù through òø...òù; call will abort at runtime

So pass a 'const char *' to sprintf instead:

sprintf (/* ... */, what_.c_str());

return buffer;
}

private:
std::string what_;

};
(yes, I know a static buffer and a sprintf() call are asking for
trouble)

It is an interesting idea though, because if one assumes that there
can only be one exception thrown at a given time; that buffer cannot
be shared by two exceptions. But, if multithreaded, there may be two
exceptions in flight at the same time to be thrown on two separate
threads and that would be trouble.

How about storing the error information as fundamental types and
returning that to the caller:

private:
double temperature_;

The caller can make use of that information in any way they want:

catch (const MyError & error) {
cout << "Error: Temperature was " << error.get_temperature() <<
'\n';
}
the compiler objects to the runtime_error::what() call
illegal call of non-static member function

That's the wrong error message.
well I know its non-static, but why is it illegal?

The above should fix it.

Ali
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top