Error handling
- Dettagli
- Categoria: TSL
- Pubblicato Lunedì, 21 Ottobre 2013 23:25
- Scritto da Giampiero Gabbiani
- Visite: 1837
During parsing of an XML or JSON file, in case of error, an exception of type tsl::ParseError is thrown.
The definition of the class is the following:
class ParseError : public std::runtime_error { public: ParseError(const std::string &msg,size_t r,size_t c); const size_t row,col; };
The two members ParseError::row and ParseError::col will contain the point of the error while the result of the std::runtime_error::what() method invocation will report the error coordinates plus a simple explanation. In the example below we try to deserialize into a std::tuple of one string and one unsigned int a collection of two strings, producing a ParseError catched and dumped to cerr:
typedef std::tuple<string,unsigned int> WrongSequence; std::stringstream stream( "{\n" // row 1 " \"correct\": \"string 1\",\n" // row 2 " \"wrong\" : \"string 2\"\n" // row 3 "}\n"); WrongSequence restored; try { stream.unsetf(ios::skipws); tsl::IJsonArchive archive(stream); archive & get<0>(restored) & get<1>(restored); } catch (const tsl::ParseError &error) { cerr << error.what() << endl << "row --> " << error.row << ",column --> " << error.col << endl; }
the result is below:
error at line 3 column 13 unmarshalling unsigned int value.
row --> 3,column --> 13