37

这是我的代码。

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

理想情况下,如果它是 std::exception,我想获取与异常关联的字符串。

4

4 回答 4

26

// 然后呢?

这是什么:

#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's 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's success!
and now what: who knows

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

所以这允许进入what包罗万象的条款。

但是如果嵌套了异常怎么办???这是什么:

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

但是如果递归太深怎么办?如果堆栈溢出怎么办?优化了什么:

#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

类似的功能可以在 C++03 中通过使用允许throw在 catch 块之外重新出现异常的技巧来实现:https ://stackoverflow.com/a/3641809/5447906

于 2016-05-14T05:03:35.287 回答
22
try
{
   std::rethrow_exception(eptr);
}
catch (const std::exception& e)
{
   std::cerr << e.what() << std::endl;
}

http://en.cppreference.com/w/cpp/error/exception_ptr

于 2013-01-09T10:15:41.390 回答
13

在您的情况下,使用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";
}
于 2013-01-09T10:30:40.533 回答
1

在我看来,这不是最好的解决方案,但似乎有效。

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 块内再次抛出。

于 2013-01-09T10:30:32.390 回答