【问题标题】:Weird issue with catching trivial boost exception捕捉微不足道的提升异常的奇怪问题
【发布时间】:2015-04-20 15:38:13
【问题描述】:

我对以下简单代码有疑问:

    try
    {
        boost::lexical_cast<unsigned int>("invalid string");
    }
    catch(::boost::bad_lexical_cast &)
    {
        //It's not catched!!!
        return;
    }
    catch (std::exception &e){
        std::cerr << boost::diagnostic_information(e) << std::endl;

        ::boost::bad_lexical_cast s;
        std::string ss = typeid(s).name();
        std::cout << "'" << s.what()<<"': '"<< ss <<"'";

        std::string ee = typeid(e).name();
        std::cout << "'" << e.what()<<"': '"<< ee <<"'";
    }

lexical_cast 抛出的 boost::bad_lexical_cast 异常与我尝试捕获的异常不同,因此第一个捕获被简单地忽略。 一个例外是以下类型:

(损坏) N5boost16exception_detail10clone_implINS0_19error_info_injectorINS_16bad_lexical_castEEEEE

那是一个:

boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_lexical_cast> >

而另一个只是boost::bad_lexical_exception

我的问题是:我怎样才能更好地调试这种情况?为什么它只发生在本地,而在另一个环境中问题没有出现?我怎样才能防止这些奇怪的行为?

感谢您的帮助!

【问题讨论】:

    标签: c++ boost exception-handling


    【解决方案1】:

    你看到的奇怪类型是boost::exception_detail::clone_impl&lt; boost::exception_detail::error_info_injector&lt; boost::bad_lexical_cast&gt;&gt;。它是 Boost.Exception 提供的 bad_lexical_cast 的包装器(并派生自),它为 boost::exception_ptr 和错误信息工具提供支持。第一次钓到就可以了。

    如果不是,这通常是不同动态库中 RTTI 信息冲突的结果。这是我唯一能想到的来解释你的测试用例的行为。

    【讨论】:

    • 是的,我也在考虑 RTTI 冲突,即使我只导入一个 boost 库版本,也必须有一些重复/双重导入。尽管如此,我仍然不知道如何追踪这个问题......我尝试了 lib 依赖项,但我没有看到它们有任何问题......
    猜你喜欢
    • 1970-01-01
    • 2011-12-16
    • 2011-12-07
    • 1970-01-01
    • 1970-01-01
    • 2012-06-21
    • 2012-01-10
    • 2010-12-11
    • 2020-04-20
    相关资源
    最近更新 更多