gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 05:10:21 25 4
gpt4 key购买 nike

有人能知道什么样的异常在“try”中返回以下代码吗?我尝试使用 3 种“捕获(异常)”,但似乎没有一个起作用。

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");

关于c++ - 尝试获取不存在的节点时如何不出现任何错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43658688/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com