invalid_iterator.cpp 469 B

123456789101112131415161718192021
  1. #include <iostream>
  2. #include <nlohmann/json.hpp>
  3. using json = nlohmann::json;
  4. int main()
  5. {
  6. try
  7. {
  8. // calling iterator::key() on non-object iterator
  9. json j = "string";
  10. json::iterator it = j.begin();
  11. auto k = it.key();
  12. }
  13. catch (json::invalid_iterator& e)
  14. {
  15. // output exception information
  16. std::cout << "message: " << e.what() << '\n'
  17. << "exception id: " << e.id << std::endl;
  18. }
  19. }