尝试获取不存在的节点时如何不出现任何错误

How to not have any error when try to get a not existing node

有人可以知道 "try" 中的以下代码有什么样的异常 return 吗? 我尝试使用 3 种 "catch (exception)",但似乎没有一个有效。

try
{
    std::cout << "try to get not existing path" << std::endl;
    std::string path = this->m_Tree.get<std::string>(PATH);
}
catch (const boost::property_tree::ptree_bad_path& e)
{
    std::cout << "ptree_bad_path" << std::endl;
}

感谢您的帮助。

查看文档:

Three Ways of Getting Data

There are three versions of get: get, get (default-value version), and get_optional, which differ by failure handling strategy. All versions take path specifier, which determines in which key to search for a value. It can be a single key, or a path to key, where path elements are separated with a special character (a '.' if not specified differently). For example debug.logging.errorlevel might be a valid path with dot as a separator.

所以,只要使用 get_optional<std::string> 我会说

ptree pt;
/* ... */
boost::optional<float> v = pt.get_optional<float>("a.path.to.float.value");