0

Could somebody knows what kind of exception return the following code inside the "try" ? I try to use the 3 kind of "catch (exception)" and no one seems to work.

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;
}

Thank you for your help.

Miwa
  • 45
  • 1
  • 1
  • 5
  • According to http://www.boost.org/doc/libs/1_64_0/doc/html/property_tree/accessing.html it will be a `ptree_bad_path` exception. Can you be more specific about what isn't working? Please provide a [mcve] that we can use to reproduce the problem. – BoBTFish Apr 27 '17 at 13:05
  • 1
    `no one seems to work` Please edit question and explain: 1- what you expect to happen, 2- what is happening, – kebs Apr 27 '17 at 13:10
  • Hi guys, I can not provide a minimal exemple, but I can explain what I expect. In fact, I would like to rise the catch part because I know my path does not exist. But my program fail just after print "try to get not existing path" – Miwa Apr 27 '17 at 13:18
  • 1
    If you cannot provide a minimal example, how will you integrate a solution? That simply means you do not how to make a program. It's weird. If you can't describe your problem well, you won't get quality answers. – sehe Apr 28 '17 at 09:42

1 Answers1

1

Look at the docs:

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.

So, just use get_optional<std::string> I'd say

ptree pt;
/* ... */
boost::optional<float> v = pt.get_optional<float>("a.path.to.float.value");
sehe
  • 374,641
  • 47
  • 450
  • 633