如何使用set_unexpected(my_unexpected())获取有关未处理异常的信息

How to retrieve info about unhandled exception using set_unexpected(my_unexpected())?

本文关键字:unexpected 未处理 异常 信息 何使用 my 获取 set      更新时间:2023-10-16

如何获得未处理异常的信息(类型+堆栈),在某个奇怪的代码中抛出,使用以下机制:

set_unexpected (my_unexpected ()) ?

我怀疑这是不可能的。是否有其他方法如何获得异常类型,在最好的情况下,甚至调用堆栈的地方,未处理的抛出是?

我又浏览了一下,发现了一些很有用的东西。

我的问题可以改写为:如何复活一个未处理的异常。下面是一个解决方案:

// resurrect gone exception here
std::string ResurrectException()
{
   try 
   {
       throw;
   } 
   catch (const std::exception& e) 
   {
       return e.what();
   }
   catch (int intEx) 
   {
       char buffer[2];       
       sprintf(buffer,"%d",intEx);
       std::string outStr = buffer;
       return buffer;
   }
   catch (std::string strEx)
   {
        return strEx;
   }
   catch (char * buffEx)
   {
       std::string outStr = buffEx;
       return outStr;
   }
   // } catch (my_custom_exception_type& e) {
   //    return e.ToString(); }
   catch(...) 
   {
       return "unknown exception!";
   }
}

详情见此处

我只是想知道"获得抛出异常的地方的堆栈"这一部分。在c#标准系统中。异常类作为字符串成员携带有关堆栈的信息。不幸的是,我在c++标准系统异常中没有看到这个