JSON for Modern C++  3.0.0

◆ out_of_range

template<template< typename, typename, typename... > class ObjectType = std::map, template< typename, typename... > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename > class AllocatorType = std::allocator, template< typename, typename=void > class JSONSerializer = adl_serializer>
using nlohmann::basic_json::out_of_range = detail::out_of_range

This exception is thrown in case a library function is called on an input parameter that exceeds the expected range, for instance in case of array indices or nonexisting object keys.

Exceptions have ids 4xx.

name / id example message description
json.exception.out_of_range.401 array index 3 is out of range The provided array index i is larger than size-1.
json.exception.out_of_range.402 array index '-' (3) is out of range The special array index - in a JSON Pointer never describes a valid element of the array, but the index past the end. That is, it can only be used to add elements at this position, but not to read it.
json.exception.out_of_range.403 key 'foo' not found The provided key was not found in the JSON object.
json.exception.out_of_range.404 unresolved reference token 'foo' A reference token in a JSON Pointer could not be resolved.
json.exception.out_of_range.405 JSON pointer has no parent The JSON Patch operations 'remove' and 'add' can not be applied to the root element of the JSON value.
json.exception.out_of_range.406 number overflow parsing '10E1000' A parsed number could not be stored as without changing it to NaN or INF.
Example
The following code shows how an out_of_range exception can be caught.
1 #include <iostream>
2 #include "json.hpp"
3 
4 using json = nlohmann::json;
5 
6 int main()
7 {
8  try
9  {
10  // calling at() for an invalid index
11  json j = {1, 2, 3, 4};
12  j.at(4) = 10;
13  }
14  catch (json::out_of_range& e)
15  {
16  // output exception information
17  std::cout << "message: " << e.what() << '\n'
18  << "exception id: " << e.id << std::endl;
19  }
20 }
basic_json<> json
default JSON class
Definition: json.hpp:14353
detail::out_of_range out_of_range
exception indicating access out of the defined range
Definition: json.hpp:7414
Output (play with this example online):
message: [json.exception.out_of_range.401] array index 4 is out of range
exception id: 401
The example code above can be translated with
g++ -std=c++11 -Isrc doc/examples/out_of_range.cpp -o out_of_range 
See also
exception for the base class of the library exceptions
parse_error for exceptions indicating a parse error
invalid_iterator for exceptions indicating errors with iterators
type_error for exceptions indicating executing a member function with a wrong type
other_error for exceptions indicating other library errors
Since
version 3.0.0

Definition at line 7414 of file json.hpp.