首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >获取boost::exception的what()消息

获取boost::exception的what()消息
EN

Stack Overflow用户
提问于 2016-04-21 10:25:02
回答 2查看 3.6K关注 0票数 6

在下面的代码中,我希望获得boost::exception的what()消息。

代码语言:javascript
运行
复制
#include <iostream>
#include <boost/lexical_cast.hpp>
#include <boost/exception/diagnostic_information.hpp>

int main(void)
{
    try
    {
        int i(boost::lexical_cast<int>("42X"));
    }
    catch (boost::exception const &e)
    {
        std::cout << "Exception: " << boost::diagnostic_information_what(e) << "\n";
    }
    return 0;
}

当我运行它时,我会收到这样的信息:

代码语言:javascript
运行
复制
Exception: Throw location unknown (consider using BOOST_THROW_EXCEPTION)
Dynamic exception type: boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_lexical_cast> >

但是,当我没有捕获异常时,shell输出:

代码语言:javascript
运行
复制
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_lexical_cast> >'
  what():  bad lexical cast: source type value could not be interpreted as target
[1]    8744 abort      ./a.out

我想要这样的信息:bad lexical cast: source type value could not be interpreted as target;但是我找不到方法去获得它。boost异常系统对我来说是个谜。

怎么才能得到这条信息?

编辑:boost::异常没有方法。那么,它怎么能写std::exception::what: bad lexical cast: source type value could not be interpreted as target,因为这不是std::exception

EN

Stack Overflow用户

回答已采纳

发布于 2016-04-21 10:36:25

将其捕获为bad_lexical_cast,以使用what()方法

代码语言:javascript
运行
复制
catch (const boost::bad_lexical_cast& e)
{      //    ^^^^^^^^^^^^^^^^^^^^^^^
    std::cout << "Exception: " << e.what() << "\n";
                              //  ^^^^^^^^
}

它将显示Exception: bad lexical cast: source type value could not be interpreted as target

票数 4
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36766740

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档