6

I'm trying to print the type name using exceptions, but my program doesn't even seem to catch the exception and instead seems to call the default termination function. What have I missed?

#include <cstdio>
#include <exception>
#include <typeinfo>

namespace Error
{
    template<typename T>
    class Blah : std::exception
    {
        virtual const char* what() const throw()
        {
            return typeid(T).name();
        }
    };
}

void blah() {
    throw Error::Blah<int*********>();
}

int main()
{
    try
    {
        blah();
    }
    catch (std::exception& e)
    {
        std::puts(e.what());
    }
}
Barry
  • 286,269
  • 29
  • 621
  • 977

1 Answers1

10

The problem is here:

template<typename T>
class Blah : std::exception
//          ^^^^^^^^^^^^^^^

You're inheriting privately (since class inheritance is private by default and you don't add a specifier), so std::exception isn't an accessible base. You have to inherit publicly.

Barry
  • 286,269
  • 29
  • 621
  • 977