Подтвердить что ты не робот

Как сделать вызов what() на std:: exception_ptr

Это код, который у меня есть.

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

В идеале я хотел бы получить строку, связанную с исключением, если она является std:: exception.

4b9b3361

Ответ 1

Использование std::current_exception кажется немного выше всего в вашем случае, так как вы, похоже, не хотите хранить или копировать std::exception_ptr для последующей обработки (что является его единственным намерением, это не помогает с получением дополнительная информация об неизвестном исключении в любом случае). Если вы просто хотите обработать случай a std::exception, то про простой:

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";
}

Ответ 3

//то что?

вот что:

#include <exception>
#include <stdexcept>
#include <iostream>
#include <string>

std::string what(const std::exception_ptr &eptr = std::current_exception())
{
    if (!eptr) { throw std::bad_exception(); }

    try { std::rethrow_exception(eptr); }
    catch (const std::exception &e) { return e.what()   ; }
    catch (const std::string    &e) { return e          ; }
    catch (const char           *e) { return e          ; }
    catch (...)                     { return "who knows"; }
}

int main()
{
    try { throw std::runtime_error("it success!"); }
    catch (...) { std::cerr << "Here is WHAT happened: " << what() << std::endl;  }

    try { throw 42; } catch (...) { std::cerr << "and now what: " << what() << std::endl;  }
}

что он печатает:

Here is WHAT happened: it success!
and now what: who knows

http://coliru.stacked-crooked.com/a/1851d2ab9faa3a24

поэтому это позволяет получить what в предложении catch-all.

но что, если исключение вложен??? вот что:

std::string what(const std::exception_ptr &eptr = std::current_exception());

template <typename T>
std::string nested_what(const T &e)
{
    try         { std::rethrow_if_nested(e); }
    catch (...) { return " (" + what(std::current_exception()) + ")"; }
    return {};
}

std::string what(const std::exception_ptr &eptr)
{
    if (!eptr) { throw std::bad_exception(); }

    try { std::rethrow_exception(eptr); }
    catch (const std::exception &e) { return e.what() + nested_what(e); }
    catch (const std::string    &e) { return e          ; }
    catch (const char           *e) { return e          ; }
    catch (...)                     { return "who knows"; }
}

используя пример из здесь:

#include <fstream>

...

// sample function that catches an exception and wraps it in a nested exception
void open_file(const std::string& s)
{
    try {
        std::ifstream file(s);
        file.exceptions(std::ios_base::failbit);
    } catch(...) {
        std::throw_with_nested( std::runtime_error("Couldn't open " + s) );
    }
}

// sample function that catches an exception and wraps it in a nested exception
void run()
{
    try {
        open_file("nonexistent.file");
    } catch(...) {
        std::throw_with_nested( std::runtime_error("run() failed") );
    }
}

int main()
{
    try { throw std::runtime_error("success!"); }
    catch (...) { std::cerr << "Here is WHAT happened: \"" << what() << '\"' << std::endl;  }

    try { run(); }
    catch (...) { std::cerr << "what happened for run: \""  << what() << '\"' << std::endl;  }
}

что напечатано:

Here is WHAT happened: "success!"
what happened for run: "run() failed (Couldn't open nonexistent.file (basic_ios::clear))"

http://coliru.stacked-crooked.com/a/901a0c19297f02b5

но что, если рекурсия слишком глубокая? Что делать, если stackoverflow? оптимизировано:

#include <typeinfo>

template <typename T>
std::exception_ptr get_nested(const T &e)
{
    try
    {
        auto &nested = dynamic_cast<const std::nested_exception&>(e);
        return nested.nested_ptr();
    }
    catch (const std::bad_cast &)
        { return nullptr; }
}

#if 0 // alternative get_nested
    std::exception_ptr get_nested()
    {
        try                                    { throw                ; }
        catch (const std::nested_exception &e) { return e.nested_ptr(); }
        catch (...)                            { return nullptr       ; }
    }
#endif

std::string what(std::exception_ptr eptr = std::current_exception())
{
    if (!eptr) { throw std::bad_exception(); }

    std::string whaaat;
    std::size_t num_nested = 0;
    next:
    {
        try
        {
            std::exception_ptr yeptr;
            std::swap(eptr, yeptr);
            std::rethrow_exception(yeptr);
        }
        catch (const std::exception &e) { whaaat += e.what()   ; eptr = get_nested(e); }
        catch (const std::string    &e) { whaaat += e          ; }
        catch (const char           *e) { whaaat += e          ; }
        catch (...)                     { whaaat += "who knows"; }

        if (eptr) { whaaat += " ("; num_nested++; goto next; }
    }
    whaaat += std::string(num_nested, ')');
    return whaaat;
}

то же самое:

Here is WHAT happened: "success!"
here is what: "run() failed (Couldn't open nonexistent.file (basic_ios::clear))"

http://coliru.stacked-crooked.com/a/32ec5af5b1d43453

UPD

Аналогичная функциональность может быть реализована в С++ 03 с помощью трюка, который позволяет re throw текущее исключение вне блока catch: fooobar.com/questions/237111/...

Ответ 4

Не лучшее решение, на мой взгляд, но, похоже, работает.

try
{
// code throws potentially unknown exception
}
catch (const std::exception& e)
{
   std::cerr << e.what() << std::endl;
}
catch (...)
{
    std::exception_ptr eptr =  std::current_exception();
        // then what ?
    LogUnknownException();
}

Благодаря ForEveR для первоначального решения, но я не уверен, хочу ли я снова добавить в блок catch.