I started using std::exception as a base class for all my exceptions recently. I could not properly override what() without putting the virtual keyword in front of it. Without the virtual keyword it always seemed to call the what() function of the base class, std::exception.
It perplexed me a little because I thought that one never needed to put virtual in front of a function when overriding it (and here is a post that seems to confirm that). But I decided to let it go and move on.
Then today while reading O'Reilly's "Safe C++" I found the author also overriding what() with the virtual keyword. He wrote...
virtual const char* what() const throw () { /* stuff */ }
Why is he overriding a function and using the virtual keyword? Is it just for "documentation" as suggested in the post I quoted above?
You do not have to put the virtual keyword in front of your override of what() in order to call the subclass implementation. Perhaps when you discovered that it was invoking the base class implementation the exception object you were referencing had been sliced via an inappropriate pass of the exception? For example, I always catch by reference (per Scott Meyers' recommendation), but if I caught by the exception value and declared the catch as a superclass of the subclass that might be thrown, then the object would be sliced when I caught it. In other words, if I had this exception subclass declared:
class my_exception : public std::exception
...
and I caught an instance of it like so:
try
{
...
throw my_exception("Some message");
}
catch (std::exception e)
{
...
}
e in the catch block would be a sliced object. You should catch an exception like this:
try
{
...
throw my_exception("Some message");
}
catch (std::exception& e)
{
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With