cpp:用省略号捕获异常并查看信息

cpp: catch exception with ellipsis and see the information

本文关键字:信息 捕获异常 省略号 cpp      更新时间:2023-10-16

我知道你可以捕获"所有异常"并通过打印异常

try
{
    //some code...
}catch(const std::exception& e) {
   cout << e.what();
}

但这只是针对从std::exception派生的异常。我想知道是否有一种方法可以从省略号捕获中获得一些信息

try
{
    //some code...
}catch(...) {
   // ??
}

如果该机制与函数的省略号相同,那么我应该能够执行类似于强制转换va_list的参数并尝试调用what()方法的操作。

我还没有试过,但如果有人知道怎么做,我会很兴奋。

从C++11及以后,您可以使用std::current_exception&c:

std::exception_ptr p;
try {
    
} catch(...) {
    p = std::current_exception();
}

然后你可以"检查";CCD_ 4;c、 尽管不是以可移植的方式。

在早期的标准中,除了用throw;重新抛出异常之外,没有在catch(...)站点交叉异常的可移植方法。

对不起,你不能这么做。您只能访问特定异常类型的catch块中的异常对象。