Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bad typeinfo name for exceptions [duplicate]

When I run the following program, I get a mangled typeinfo name.

#include <iostream>
#include <stdexcept>
#include <typeinfo>

namespace std
{

class really_out_of_range
    : public out_of_range
{
public:
    explicit really_out_of_range(const string& __s) : out_of_range(__s) {}
    explicit really_out_of_range(const char* __s)   : out_of_range(__s) {}

    virtual ~really_out_of_range() _NOEXCEPT {}
};

}

void test () noexcept(false)
{
    throw std::really_out_of_range("x > 20");
}

int main () noexcept(true)
{
    try {
        test();
    } catch (const std::exception& e) {
        std::cout << "Exception caught: " << typeid(e).name() << ": " << e.what() << '\n';
    } catch (...) {
        std::cout << "Unknown exception caught\n";
    }
}

Here is the output:

Exception caught: St19really_out_of_range: x > 20

However, when I change the noexcept specification on the test() function to true in order to trigger a call to std::terminate(), I get this output:

libc++abi.dylib: terminating with uncaught exception of type std::really_out_of_range: x > 20

I thought that std::terminate() might be able to provide the unmangled type named because it explicitly handled each standard exception type, but that's clearly not the case because it also correctly handles the new exception type that I defined above.

So, my question is why is the typeinfo name correct in std::terminate(), but not when I try to access it directly? Or, more to the point, what function does std::terminate() call that provides an unmangled class name?

like image 231
IntellectualKitty Avatar asked May 24 '26 01:05

IntellectualKitty


1 Answers

After cdhowie's comment, I found the answer here: Unmangling the result of std::type_info::name

Basically, you #include <cxxabi.h> and call abi::__cxa_demangle if you're using GNU. But you have to be careful with the memory management.

However, you can also use boost. Then you just #include <boost/core/demangle.hpp> and call boost::core::demangle( name ).

.

like image 85
IntellectualKitty Avatar answered May 26 '26 13:05

IntellectualKitty



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!