【问题标题】:Catch exception in boost::property_tree::read_xml在 boost::property_tree::read_xml 中捕获异常
【发布时间】:2014-10-10 13:24:36
【问题描述】:

我正在尝试捕获由boost::property_tree::xml_parser::read_xml 引发的异常。这是我的示例程序:

 #include <iostream>
 #include <boost/property_tree/ptree.hpp>
 #include <boost/property_tree/xml_parser.hpp>
 int main(int argc, char** argv){
      boost::property_tree::ptree pt;
      std::cout<<"123123"<<std::endl;
      try{
        std::cout<<"Reading "<<argv[1]<<std::endl;
        read_xml(argv[1], pt);
      }
      catch(const boost::property_tree::xml_parser::xml_parser_error& ex){
        std::cout<<argv[1]<<" failed, reading "<<argv[2]<<std::endl;
        read_xml(argv[2], pt);
      }
 }

输出是:

123123
Reading 123.xml
[1]    97028 abort      ./a.out 123.xml 345.xml

我做错了什么?显然异常没有被捕获。我还尝试通过 (...) 捕获所有内容并捕获 std::exceptionboost::exception。在所有情况下,结果都是一样的。根据 boost 文档,应该抛出一个 xml_parser_error 。我的 boost 版本是 1.54。

一点更新: 当我不尝试只执行 read_xml("notAValidFilePAth",pt) 时,我会得到

terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::property_tree::xml_parser::xml_parser_error> >'
  what():  123.xml: cannot open file

当我使用 gdb 运行它时,我得到:

Reading terminate called after throwing an instance of 
'boost::exception_detail::clone_impl<
       boost::exception_detail::error_info_injector<
           boost::property_tree::xml_parser::xml_parser_error> >'
  what():  PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/local/MacGPG2/bin:/opt/local/bin:/usr/texbin:/Users/weidenka/bin: 
 cannot open file
 Program received signal SIGABRT, Aborted.
    0x00007fff8a31b866 in __pthread_kill ()

#0  0x00007fff8a31b866 in __pthread_kill ()
#1  0x00007fff86c9f35c in pthread_kill ()
#2  0x00007fff86240b1a in abort ()
#3  0x00000001005547d5 in __gnu_cxx::__verbose_terminate_handler ()

提前致谢!

祝你好运,彼得

【问题讨论】:

  • 这看起来像是崩溃而不是异常。使用调试器并向我们展示堆栈跟踪。
  • 如果它崩溃了,这看起来像是库中的一个错误(除非有任何错误的构建 w.r.t. 库兼容性)

标签: c++ boost


【解决方案1】:

这是你的问题:

    read_xml(argv[2], pt);

回退读取操作不会引发异常,因此当此时发生异常时,您的程序将终止而不刷新 iostreams 缓冲区。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-21
    • 2011-08-21
    • 2018-01-04
    相关资源
    最近更新 更多