Sunday 15 January 2012

c++ - Catch exception in boost::property_tree::read_xml -



c++ - Catch exception in boost::property_tree::read_xml -

i'm trying grab exceptions raised boost::property_tree::xml_parser::read_xml. here illustration program:

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

the output is:

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

what doing wrong? exception isn't caught. tried grab every thing via (...) , grab std::exception boost::exception. in cases result same. according boost documentation xml_parser_error should thrown. boost version 1.54.

a little update: when don't seek execute read_xml("notavalidfilepath",pt), get

terminate called after throwing 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

and when run gdb get:

reading terminate called after throwing 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 programme 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 ()

thanks in advance!

best wishes, peter

this problem:

read_xml(argv[2], pt);

the fallback read operation not guarded against throwing exception, when exception occurs @ point programme terminates without flushing iostreams buffers.

c++ boost

No comments:

Post a Comment