Exception + multiple inheritance question...

B

barcaroller

The boost::exception tutorial states that the user should derive from both
std::exception and boost::exception, as show below:


struct exception_base: virtual std::exception,
virtual boost::exception
{
};


The tutorial then shows how to catch one such exception, as shown below:

catch (exception_base& e)
{
// take action
// can I call what()?
}


My question is: how will I know that I received a std::exception object so
that I can call its what() member function (which boost::exception does not
have)?
 
I

Ian Collins

barcaroller said:
The boost::exception tutorial states that the user should derive from both
std::exception and boost::exception, as show below:


struct exception_base: virtual std::exception,
virtual boost::exception
{
};


The tutorial then shows how to catch one such exception, as shown below:

catch (exception_base& e)

Not const exception_base& e?
{
// take action
// can I call what()?
}


My question is: how will I know that I received a std::exception object so
that I can call its what() member function (which boost::exception does not
have)?

exception_base "is a" std::exception.
 
B

barcaroller

Ian Collins said:
Not const exception_base& e?

No, because the boost::exception objects could be modified/updated and then
re-thrown.

exception_base "is a" std::exception.

So what happens when the code throws a boost::exception?


foo()
{
throw boost::exception;
}

bar()
{
try
{
foo();
}
catch (exception_base& e)
{
cout << e.what() << endl; // what happens now?
}
}
 
B

barcaroller

Bo Persson said:
Then it isn't caught, because it is not an exception_base. The inheritance
goes in the other direction.

Exactly; that is why I think that the author's catch clause is not enough.
So what would be an appropriate catch clause? The code below does not
strike me as correct; why even bother deriving from std::exception and
boost::exception?


catch (exception_base& e)
{
// take some action
}
catch (std::exception& e)
{
cerr << e.what() << endl
}
catch (boost::exception& e)
{
if (string const* fn=get_error_info<string>(e))
cerr << *fn << endl;
}
 
B

Bo Persson

barcaroller said:
Exactly; that is why I think that the author's catch clause is not
enough. So what would be an appropriate catch clause? The code
below does not strike me as correct; why even bother deriving from
std::exception and boost::exception?


catch (exception_base& e)
{
// take some action
}
catch (std::exception& e)
{
cerr << e.what() << endl
}
catch (boost::exception& e)
{
if (string const* fn=get_error_info<string>(e))
cerr << *fn << endl;
}

I don't know the intention of the author - could be to combine the
functionality of both exception classes, or just to show a cool
example.

Your three catch clauses will catch anything derived from either of
the base classes. Seems kind of ok to me (but a bit weird to have both
the base classes and the derived class in the list).



Bo Persson
 
J

James Kanze

I don't know the intention of the author - could be to combine
the functionality of both exception classes, or just to show a
cool example.
Your three catch clauses will catch anything derived from
either of the base classes. Seems kind of ok to me (but a bit
weird to have both the base classes and the derived class in
the list).

Unless the derived class provides some additional details not
available in the base classes. Or represented some special type
of error, which needed separate treatment. (I would expect the
latter to be a rather frequent phenomena.)
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top