为什么这个程序无法捕获异常?

Why does this program fail to catch an exception?

我正在尝试使用异常打印类型名称,但我的程序似乎甚至没有捕获异常,而是似乎调用了默认终止函数。我错过了什么?

#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());
    }
}

问题出在这里:

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

您正在 私有地 继承(因为 class 继承默认为 private 并且您没有添加说明符),所以 std::exception 不是可访问的基础。你必须公开继承。