How do I make a call to what() on std::exception_ptr

前端 未结 4 1757
别那么骄傲
别那么骄傲 2020-12-28 13:26

This is the code I have.

try
{
// code throws potentially unknown exception
}
catch (...)
{
    std::exception_ptr eptr =  std::current_exception();
             


        
4条回答
  •  南方客
    南方客 (楼主)
    2020-12-28 14:05

    Using std::current_exception seems a bit over the top in your case, since you don't seem to want to store or copy the std::exception_ptr for later processing (which is its only intent, it doesn't help with gaining additional information about an unknown exception in any way). If you just want to treat the case of a std::exception, what about the simple:

    try
    {
        // code throws potentially unknown exception
    }
    catch (const std::exception &e)
    {
        std::cerr << e.what() << '\n';  // or whatever
    }
    catch (...)
    {
        // well ok, still unknown what to do now, 
        // but a std::exception_ptr doesn't help the situation either.
        std::cerr << "unknown exception\n";
    }
    

提交回复
热议问题