cpp: catch exception with ellipsis and see the information

若如初见. 提交于 2020-01-10 04:20:07

问题


I know that you can catch "all exceptions" and print the exception by

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

but this is just for exceptions derived from std::exception. I was wondering if there is a way to get some information from an ellipsis catch

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

If the mechanism is the same as ellipsis for functions then I should be able to do something like casting the argument of the va_list and trying to call the what() method.

I haven't tried it yet but if someone knows the way I'd be excited to know how.


回答1:


Sorry, you can't do that. You can only access the exception object in a catch block for a specific exception type.




回答2:


From C++11 and onwards, you can use std::current_exception &c:

std::exception_ptr p;
try {

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

You can then "inspect" p by taking casts &c.

In earlier standards there is no portable way of inspecting the exception at a catch(...) site.



来源:https://stackoverflow.com/questions/25642621/cpp-catch-exception-with-ellipsis-and-see-the-information

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!