C++ Exceptions and Inheritance from std::exception
- by fbrereto
Given this sample code:
#include <iostream>
#include <stdexcept>
class my_exception_t : std::exception
{
public:
explicit my_exception_t()
{ }
virtual const char* what() const throw()
{ return "Hello, world!"; }
};
int main()
{
try
{ throw my_exception_t(); }
catch (const std::exception& error)
{ std::cerr << "Exception: " << error.what() << std::endl; }
catch (...)
{ std::cerr << "Exception: unknown" << std::endl; }
return 0;
}
I get the following output:
Exception: unknown
Yet simply making the inheritance of my_exception_t from std::exception public, I get the following output:
Exception: Hello, world!
Could someone please explain to me why the type of inheritance matters in this case? Bonus points for a reference in the standard.