如何调用std :: exception_ptr上的what() [英] How do I make a call to what() on std::exception_ptr

查看:788
本文介绍了如何调用std :: exception_ptr上的what()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码。

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

理想情况下,我想获取与异常相关的字符串a std :: exception。

Ideally, I would like to get the string associated to the exception if it is a std::exception.

推荐答案

使用 std :: current_exception 在你的case顶部,因为你似乎不想存储或复制 std :: exception_ptr 用于以后的处理(这是它唯一的意图,它没有帮助获得有关未知异常的任何方式的额外信息)。如果你只是想处理 std :: exception 的情况,那么简单:

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

这篇关于如何调用std :: exception_ptr上的what()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆