Why can't this program catch an exception?

I'm trying to print the type name with exceptions, but my program does not even seem to catch the exception and instead seems to call the default termination function. What did I miss?

#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()); } } 
+6
source share
1 answer

The problem is here:

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

It is inherited confidentially (since class inheritance is private by default and you do not add a specifier), therefore std::exception not an accessible base. You must inherit in public.

+10
source

Source: https://habr.com/ru/post/985469/


All Articles