In brief:
std::ifstream is;
is.exceptions(
is.exceptions()
| std::ios_base::failbit
| std::ios_base::badbit
); // handle different exceptions as 'file not found', 'permission denied'
try {
is.open("my_valid_file.json");
const auto &jsonFile = nlohmann::json::parse(is); // (*)
// ...
catch (const std::ios_base::failure &e) {
std::cerr << e.code().message() << std::endl;
}
The line (*) produce std::ios_base::failure exception when parse function reaches end of stream even if json document is valid. Without std::ios_base::failbit function works OK.
In brief:
std::ifstream is; is.exceptions( is.exceptions() | std::ios_base::failbit | std::ios_base::badbit ); // handle different exceptions as 'file not found', 'permission denied' try { is.open("my_valid_file.json"); const auto &jsonFile = nlohmann::json::parse(is); // (*) // ... catch (const std::ios_base::failure &e) { std::cerr << e.code().message() << std::endl; }The line (*) produce std::ios_base::failure exception when parse function reaches end of stream even if json document is valid. Without std::ios_base::failbit function works OK.