Re: Overriding std::exception
On Apr 17, 2:15 am, pelio <pe...@liticom.ar> wrote:
Miles dixit:
Hello all,
When I subclass std::exception and throw an instance of the subclass,
when I call the what() method of the caught exception, it does not
call the overridden method. I'm under the impression that
std::exception declares what() as a virtual method, so I'm at a loss
as to why the subclass's what() is not being called. For example, for=
this program:
http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.10
Thank you for your response, but that doesn't explain what I'm
seeing. The FAQ entry is about throwing a reference to an object when
the apparent type of an object is different from its actual type at
the point at which it is thrown. I know that an object of the correct
type is being thrown, because std::exception::what returns the
typeid().name() of the object of the correct class.
try {
throw Exception2();
} catch (std::exception &e) {
std::cerr << e.what() << std::endl;
}
In that catch block, e is a reference to an Exception2 object (I've
verified this with typeid()), but its what() method is not being
called--it is behaving as if what() were not virtual, but according to
everything I can find, std::exception::what() *is* virtual.
Thanks,
Miles