Determining exception type after the exception is caught?

前端 未结 9 1240
梦毁少年i
梦毁少年i 2020-12-01 01:20

Is there a way to determine the exception type even know you caught the exception with a catch all?

Example:

try
{
   SomeBigFunction();
}
catch(...)         


        
9条回答
  •  醉梦人生
    2020-12-01 02:05

    There is no standard, portable way to do this. Here's a non-portable way to do it on GCC and clang

    #include 
    #include 
    
    const char* currentExceptionTypeName()
    {
        int status;
        return abi::__cxa_demangle(abi::__cxa_current_exception_type()->name(), 0, 0, &status);
    }
    
    int main()
    {
        try {
            throw std::string();
        } catch (...) {
            std::cout<<"Type of caught exception is "<

    Output:

    Type of caught exception is std::__cxx11::basic_string, std::allocator >
    

提交回复
热议问题