【问题标题】:Prevent boost xml Parser from crashing when wrong file selected in C++在 C++ 中选择错误文件时防止 boost xml Parser 崩溃
【发布时间】:2018-10-17 08:49:34
【问题描述】:

我有一个工具,用户可以通过 Qt Gui 选择 xml 来设置一些参数。问题是。当文件不是有效的 xml 文件时,我的工具就会崩溃。我在 Qt Creator 中收到以下错误:

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():  /home/path/to/Executable: expected <
10:40:44: The program has unexpectedly finished.

我可以检查 XML 是否有效,或者我可以捕获错误并执行其他操作而不是终止吗?

我想显示一条错误消息并取消解析。

我的阅读过程代码是:

#include <boost/property_tree/xml_parser.hpp>
#include <boost/property_tree/ptree.hpp>
...
using boost::property_tree::ptree;
ptree pt;
read_xml(filename, pt);

【问题讨论】:

  • 这是一个 C++ 异常被抛出,因此你可以catch它。

标签: c++ xml qt boost


【解决方案1】:

根据documentation read_xml 抛出一个xml_parser_error 以防出错,所以抓住它。

【讨论】:

  • 所以我可以使用catch (exception&amp; xml_parser_error)?
  • 哦。谢谢。可以在任何地方捕获异常吗?我想在我的 Qt MainWindow 中捕捉它。
  • 当然可以。
  • 我现在错过了什么?我的 IDE 出现错误:unknown type name 'xml_parser_error'
  • 命名空间?是boost::property_tree::xml_parser::xml_parser_error
猜你喜欢
  • 1970-01-01
  • 2014-07-06
  • 1970-01-01
  • 1970-01-01
  • 2014-11-09
  • 2014-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多