1# Exceptions 2 3## Overview 4 5### Base type 6 7All exceptions inherit from class `json::exception` (which in turn inherits from `std::exception`). It is used as the base class for all exceptions thrown by the `basic_json` class. This class can hence be used as "wildcard" to catch exceptions. 8 9```plantuml 10std::exception <|-- json::exception 11json::exception <|-- json::parse_error 12json::exception <|-- json::invalid_iterator 13json::exception <|-- json::type_error 14json::exception <|-- json::out_of_range 15json::exception <|-- json::other_error 16 17interface std::exception {} 18 19class json::exception { 20 + const int id 21 + const char* what() const 22} 23 24class json::parse_error { 25 + const std::size_t byte 26} 27``` 28 29### Switch off exceptions 30 31Exceptions are used widely within the library. They can, however, be switched off with either using the compiler flag `-fno-exceptions` or by defining the symbol [`JSON_NOEXCEPTION`](../api/macros/json_noexception.md). In this case, exceptions are replaced by `abort()` calls. You can further control this behavior by defining `JSON_THROW_USER` (overriding `#!cpp throw`), `JSON_TRY_USER` (overriding `#!cpp try`), and `JSON_CATCH_USER` (overriding `#!cpp catch`). 32 33Note that [`JSON_THROW_USER`](../api/macros/json_throw_user.md) should leave the current scope (e.g., by throwing or aborting), as continuing after it may yield undefined behavior. 34 35??? example 36 37 The code below switches off exceptions and creates a log entry with a detailed error message in case of errors. 38 39 ```cpp 40 #include <iostream> 41 42 #define JSON_TRY_USER if(true) 43 #define JSON_CATCH_USER(exception) if(false) 44 #define JSON_THROW_USER(exception) \ 45 {std::clog << "Error in " << __FILE__ << ":" << __LINE__ \ 46 << " (function " << __FUNCTION__ << ") - " \ 47 << (exception).what() << std::endl; \ 48 std::abort();} 49 50 #include <nlohmann/json.hpp> 51 ``` 52 53Note the explanatory [`what()`](https://en.cppreference.com/w/cpp/error/exception/what) string of exceptions is not available for MSVC if exceptions are disabled, see [#2824](https://github.com/nlohmann/json/discussions/2824). 54 55See [documentation of `JSON_TRY_USER`, `JSON_CATCH_USER` and `JSON_THROW_USER`](../api/macros/json_throw_user.md) for more information. 56 57### Extended diagnostic messages 58 59Exceptions in the library are thrown in the local context of the JSON value they are detected. This makes detailed diagnostics messages, and hence debugging, difficult. 60 61??? example 62 63 ```cpp 64 --8<-- "examples/diagnostics_standard.cpp" 65 ``` 66 67 Output: 68 69 ``` 70 --8<-- "examples/diagnostics_standard.output" 71 ``` 72 73 This exception can be hard to debug if storing the value `#!c "12"` and accessing it is further apart. 74 75To create better diagnostics messages, each JSON value needs a pointer to its parent value such that a global context (i.e., a path from the root value to the value that lead to the exception) can be created. That global context is provided as [JSON Pointer](../features/json_pointer.md). 76 77As this global context comes at the price of storing one additional pointer per JSON value and runtime overhead to maintain the parent relation, extended diagnostics are disabled by default. They can, however, be enabled by defining the preprocessor symbol [`JSON_DIAGNOSTICS`](../api/macros/json_diagnostics.md) to `1` before including `json.hpp`. 78 79??? example 80 81 ```cpp 82 --8<-- "examples/diagnostics_extended.cpp" 83 ``` 84 85 Output: 86 87 ``` 88 --8<-- "examples/diagnostics_extended.output" 89 ``` 90 91 Now the exception message contains a JSON Pointer `/address/housenumber` that indicates which value has the wrong type. 92 93See [documentation of `JSON_DIAGNOSTICS`](../api/macros/json_diagnostics.md) for more information. 94 95## Parse errors 96 97This exception is thrown by the library when a parse error occurs. Parse errors 98can occur during the deserialization of JSON text, CBOR, MessagePack, as well 99as when using JSON Patch. 100 101Exceptions have ids 1xx. 102 103!!! info "Byte index" 104 105 Member `byte` holds the byte index of the last read character in the input 106 file. 107 108 For an input with n bytes, 1 is the index of the first character and n+1 109 is the index of the terminating null byte or the end of file. This also 110 holds true when reading a byte vector (CBOR or MessagePack). 111 112??? example 113 114 The following code shows how a `parse_error` exception can be caught. 115 116 ```cpp 117 --8<-- "examples/parse_error.cpp" 118 ``` 119 120 Output: 121 122 ``` 123 --8<-- "examples/parse_error.output" 124 ``` 125 126 127### json.exception.parse_error.101 128 129This error indicates a syntax error while deserializing a JSON text. The error message describes that an unexpected token (character) was encountered, and the member `byte` indicates the error position. 130 131!!! failure "Example message" 132 133 Input ended prematurely: 134 135 ``` 136 [json.exception.parse_error.101] parse error at 2: unexpected end of input; expected string literal 137 ``` 138 139 No input: 140 141 ``` 142 [json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal 143 ``` 144 145 Control character was not escaped: 146 147 ``` 148 [json.exception.parse_error.101] parse error at line 1, column 2: syntax error while parsing value - invalid string: control character U+0009 (HT) must be escaped to \u0009 or \\; last read: '"<U+0009>'" 149 ``` 150 151 String was not closed: 152 153 ``` 154 [json.exception.parse_error.101] parse error at line 1, column 2: syntax error while parsing value - invalid string: missing closing quote; last read: '"' 155 ``` 156 157 Invalid number format: 158 159 ``` 160 [json.exception.parse_error.101] parse error at line 1, column 3: syntax error while parsing value - invalid number; expected '+', '-', or digit after exponent; last read: '1E' 161 ``` 162 163 `\u` was not be followed by four hex digits: 164 165 ``` 166 [json.exception.parse_error.101] parse error at line 1, column 6: syntax error while parsing value - invalid string: '\u' must be followed by 4 hex digits; last read: '"\u01"' 167 ``` 168 169 Invalid UTF-8 surrogate pair: 170 171 ``` 172 [json.exception.parse_error.101] parse error at line 1, column 13: syntax error while parsing value - invalid string: surrogate U+DC00..U+DFFF must follow U+D800..U+DBFF; last read: '"\uD7FF\uDC00'" 173 ``` 174 175 Invalid UTF-8 byte: 176 177 ``` 178 [json.exception.parse_error.101] parse error at line 3, column 24: syntax error while parsing value - invalid string: ill-formed UTF-8 byte; last read: '"vous \352t' 179 ``` 180 181!!! tip 182 183 - Make sure the input is correctly read. Try to write the input to standard output to check if, for instance, the input file was successfully opened. 184 - Paste the input to a JSON validator like <http://jsonlint.com> or a tool like [jq](https://stedolan.github.io/jq/). 185 186### json.exception.parse_error.102 187 188JSON uses the `\uxxxx` format to describe Unicode characters. Code points above 0xFFFF are split into two `\uxxxx` entries ("surrogate pairs"). This error indicates that the surrogate pair is incomplete or contains an invalid code point. 189 190!!! failure "Example message" 191 192 ``` 193 parse error at 14: missing or wrong low surrogate 194 ``` 195 196!!! note 197 198 This exception is not used any more. Instead [json.exception.parse_error.101](#jsonexceptionparse_error101) with a more detailed description is used. 199 200### json.exception.parse_error.103 201 202Unicode supports code points up to 0x10FFFF. Code points above 0x10FFFF are invalid. 203 204!!! failure "Example message" 205 206 ``` 207 parse error: code points above 0x10FFFF are invalid 208 ``` 209 210!!! note 211 212 This exception is not used any more. Instead [json.exception.parse_error.101](#jsonexceptionparse_error101) with a more detailed description is used. 213 214### json.exception.parse_error.104 215 216[RFC 6902](https://tools.ietf.org/html/rfc6902) requires a JSON Patch document to be a JSON document that represents an array of objects. 217 218!!! failure "Example message" 219 220 ``` 221 [json.exception.parse_error.104] parse error: JSON patch must be an array of objects 222 ``` 223 224### json.exception.parse_error.105 225 226An operation of a JSON Patch document must contain exactly one "op" member, whose value indicates the operation to perform. Its value must be one of "add", "remove", "replace", "move", "copy", or "test"; other values are errors. 227 228!!! failure "Example message" 229 230 ``` 231 [json.exception.parse_error.105] parse error: operation 'add' must have member 'value' 232 ``` 233 ``` 234 [json.exception.parse_error.105] parse error: operation 'copy' must have string member 'from' 235 ``` 236 ``` 237 [json.exception.parse_error.105] parse error: operation value 'foo' is invalid 238 ``` 239 240### json.exception.parse_error.106 241 242An array index in a JSON Pointer ([RFC 6901](https://tools.ietf.org/html/rfc6901)) may be `0` or any number without a leading `0`. 243 244!!! failure "Example message" 245 246 ``` 247 [json.exception.parse_error.106] parse error: array index '01' must not begin with '0' 248 ``` 249 250### json.exception.parse_error.107 251 252A JSON Pointer must be a Unicode string containing a sequence of zero or more reference tokens, each prefixed by a `/` character. 253 254!!! failure "Example message" 255 256 ``` 257 [json.exception.parse_error.107] parse error at byte 1: JSON pointer must be empty or begin with '/' - was: 'foo' 258 ``` 259 260### json.exception.parse_error.108 261 262In a JSON Pointer, only `~0` and `~1` are valid escape sequences. 263 264!!! failure "Example message" 265 266 ``` 267 [json.exception.parse_error.108] parse error: escape character '~' must be followed with '0' or '1' 268 ``` 269 270### json.exception.parse_error.109 271 272A JSON Pointer array index must be a number. 273 274!!! failure "Example messages" 275 276 ``` 277 [json.exception.parse_error.109] parse error: array index 'one' is not a number 278 ``` 279 ``` 280 [json.exception.parse_error.109] parse error: array index '+1' is not a number 281 ``` 282 283### json.exception.parse_error.110 284 285When parsing CBOR or MessagePack, the byte vector ends before the complete value has been read. 286 287!!! failure "Example message" 288 289 ``` 290 [json.exception.parse_error.110] parse error at byte 5: syntax error while parsing CBOR string: unexpected end of input 291 ``` 292 ``` 293 [json.exception.parse_error.110] parse error at byte 2: syntax error while parsing UBJSON value: expected end of input; last byte: 0x5A 294 ``` 295 296### json.exception.parse_error.112 297 298An unexpected byte was read in a [binary format](../features/binary_formats/index.md) or length information is invalid ([BSON](../features/binary_formats/bson.md)). 299 300!!! failure "Example messages" 301 302 ``` 303 [json.exception.parse_error.112] parse error at byte 1: syntax error while parsing CBOR value: invalid byte: 0x1C 304 ``` 305 ``` 306 [json.exception.parse_error.112] parse error at byte 1: syntax error while parsing MessagePack value: invalid byte: 0xC1 307 ``` 308 ``` 309 [json.exception.parse_error.112] parse error at byte 4: syntax error while parsing BJData size: expected '#' after type information; last byte: 0x02 310 ``` 311 ``` 312 [json.exception.parse_error.112] parse error at byte 4: syntax error while parsing UBJSON size: expected '#' after type information; last byte: 0x02 313 ``` 314 ``` 315 [json.exception.parse_error.112] parse error at byte 10: syntax error while parsing BSON string: string length must be at least 1, is -2147483648 316 ``` 317 ``` 318 [json.exception.parse_error.112] parse error at byte 15: syntax error while parsing BSON binary: byte array length cannot be negative, is -1 319 ``` 320 321### json.exception.parse_error.113 322 323While parsing a map key, a value that is not a string has been read. 324 325!!! failure "Example messages" 326 327 ``` 328 [json.exception.parse_error.113] parse error at byte 2: syntax error while parsing CBOR string: expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0xFF 329 ``` 330 ``` 331 [json.exception.parse_error.113] parse error at byte 2: syntax error while parsing MessagePack string: expected length specification (0xA0-0xBF, 0xD9-0xDB); last byte: 0xFF 332 ``` 333 ``` 334 [json.exception.parse_error.113] parse error at byte 2: syntax error while parsing UBJSON char: byte after 'C' must be in range 0x00..0x7F; last byte: 0x82 335 ``` 336 337### json.exception.parse_error.114 338 339The parsing of the corresponding BSON record type is not implemented (yet). 340 341!!! failure "Example message" 342 343 ``` 344 [json.exception.parse_error.114] parse error at byte 5: Unsupported BSON record type 0xFF 345 ``` 346 347### json.exception.parse_error.115 348 349A UBJSON high-precision number could not be parsed. 350 351!!! failure "Example message" 352 353 ``` 354 [json.exception.parse_error.115] parse error at byte 5: syntax error while parsing UBJSON high-precision number: invalid number text: 1A 355 ``` 356 357## Iterator errors 358 359This exception is thrown if iterators passed to a library function do not match 360the expected semantics. 361 362Exceptions have ids 2xx. 363 364??? example 365 366 The following code shows how an `invalid_iterator` exception can be caught. 367 368 ```cpp 369 --8<-- "examples/invalid_iterator.cpp" 370 ``` 371 372 Output: 373 374 ``` 375 --8<-- "examples/invalid_iterator.output" 376 ``` 377 378### json.exception.invalid_iterator.201 379 380The iterators passed to constructor `basic_json(InputIT first, InputIT last)` are not compatible, meaning they do not belong to the same container. Therefore, the range (`first`, `last`) is invalid. 381 382!!! failure "Example message" 383 384 ``` 385 [json.exception.invalid_iterator.201] iterators are not compatible 386 ``` 387 388### json.exception.invalid_iterator.202 389 390In the [erase](../api/basic_json/erase.md) or insert function, the passed iterator `pos` does not belong to the JSON value for which the function was called. It hence does not define a valid position for the deletion/insertion. 391 392!!! failure "Example messages" 393 394 ``` 395 [json.exception.invalid_iterator.202] iterator does not fit current value 396 ``` 397 ``` 398 [json.exception.invalid_iterator.202] iterators first and last must point to objects 399 ``` 400 401### json.exception.invalid_iterator.203 402 403Either iterator passed to function [`erase(IteratorType first, IteratorType last`)](../api/basic_json/erase.md) does not belong to the JSON value from which values shall be erased. It hence does not define a valid range to delete values from. 404 405!!! failure "Example message" 406 407 ``` 408 [json.exception.invalid_iterator.203] iterators do not fit current value 409 ``` 410 411### json.exception.invalid_iterator.204 412 413When an iterator range for a primitive type (number, boolean, or string) is passed to a constructor or an [erase](../api/basic_json/erase.md) function, this range has to be exactly (`begin(),` `end()),` because this is the only way the single stored value is expressed. All other ranges are invalid. 414 415!!! failure "Example message" 416 417 ``` 418 [json.exception.invalid_iterator.204] iterators out of range 419 ``` 420 421### json.exception.invalid_iterator.205 422 423When an iterator for a primitive type (number, boolean, or string) is passed to an [erase](../api/basic_json/erase.md) function, the iterator has to be the `begin()` iterator, because it is the only way to address the stored value. All other iterators are invalid. 424 425!!! failure "Example message" 426 427 ``` 428 [json.exception.invalid_iterator.205] iterator out of range 429 ``` 430 431### json.exception.invalid_iterator.206 432 433The iterators passed to constructor `basic_json(InputIT first, InputIT last)` belong to a JSON null value and hence to not define a valid range. 434 435!!! failure "Example message" 436 437 ``` 438 [json.exception.invalid_iterator.206] cannot construct with iterators from null 439 ``` 440 441### json.exception.invalid_iterator.207 442 443The `key()` member function can only be used on iterators belonging to a JSON object, because other types do not have a concept of a key. 444 445!!! failure "Example message" 446 447 ``` 448 [json.exception.invalid_iterator.207] cannot use key() for non-object iterators 449 ``` 450 451 452### json.exception.invalid_iterator.208 453 454The `operator[]` to specify a concrete offset cannot be used on iterators belonging to a JSON object, because JSON objects are unordered. 455 456!!! failure "Example message" 457 458 ``` 459 [json.exception.invalid_iterator.208] cannot use operator[] for object iterators 460 ``` 461 462### json.exception.invalid_iterator.209 463 464The offset operators (`+`, `-`, `+=`, `-=`) cannot be used on iterators belonging to a JSON object, because JSON objects are unordered. 465 466!!! failure "Example message" 467 468 ``` 469 [json.exception.invalid_iterator.209] cannot use offsets with object iterators 470 ``` 471 472### json.exception.invalid_iterator.210 473 474The iterator range passed to the insert function are not compatible, meaning they do not belong to the same container. Therefore, the range (`first`, `last`) is invalid. 475 476!!! failure "Example message" 477 478 ``` 479 [json.exception.invalid_iterator.210] iterators do not fit 480 ``` 481 482### json.exception.invalid_iterator.211 483 484The iterator range passed to the insert function must not be a subrange of the container to insert to. 485 486!!! failure "Example message" 487 488 ``` 489 [json.exception.invalid_iterator.211] passed iterators may not belong to container 490 ``` 491 492### json.exception.invalid_iterator.212 493 494When two iterators are compared, they must belong to the same container. 495 496!!! failure "Example message" 497 498 ``` 499 [json.exception.invalid_iterator.212] cannot compare iterators of different containers 500 ``` 501 502### json.exception.invalid_iterator.213 503 504The order of object iterators cannot be compared, because JSON objects are unordered. 505 506!!! failure "Example message" 507 508 ``` 509 [json.exception.invalid_iterator.213] cannot compare order of object iterators 510 ``` 511 512### json.exception.invalid_iterator.214 513 514Cannot get value for iterator: Either the iterator belongs to a null value or it is an iterator to a primitive type (number, boolean, or string), but the iterator is different to `begin()`. 515 516!!! failure "Example message" 517 518 ``` 519 [json.exception.invalid_iterator.214] cannot get value 520 ``` 521 522## Type errors 523 524This exception is thrown in case of a type error; that is, a library function is executed on a JSON value whose type does not match the expected semantics. 525 526Exceptions have ids 3xx. 527 528??? example 529 530 The following code shows how a `type_error` exception can be caught. 531 532 ```cpp 533 --8<-- "examples/type_error.cpp" 534 ``` 535 536 Output: 537 538 ``` 539 --8<-- "examples/type_error.output" 540 ``` 541 542### json.exception.type_error.301 543 544To create an object from an initializer list, the initializer list must consist only of a list of pairs whose first element is a string. When this constraint is violated, an array is created instead. 545 546!!! failure "Example message" 547 548 ``` 549 [json.exception.type_error.301] cannot create object from initializer list 550 ``` 551 552### json.exception.type_error.302 553 554During implicit or explicit value conversion, the JSON type must be compatible to the target type. For instance, a JSON string can only be converted into string types, but not into numbers or boolean types. 555 556!!! failure "Example messages" 557 558 ``` 559 [json.exception.type_error.302] type must be object, but is null 560 ``` 561 ``` 562 [json.exception.type_error.302] type must be string, but is object 563 ``` 564 565### json.exception.type_error.303 566 567To retrieve a reference to a value stored in a `basic_json` object with `get_ref`, the type of the reference must match the value type. For instance, for a JSON array, the `ReferenceType` must be `array_t &`. 568 569!!! failure "Example messages" 570 571 ``` 572 [json.exception.type_error.303] incompatible ReferenceType for get_ref, actual type is object 573 ``` 574 ``` 575 [json.exception.type_error.303] incompatible ReferenceType for get_ref, actual type is number" 576 ``` 577 578### json.exception.type_error.304 579 580The `at()` member functions can only be executed for certain JSON types. 581 582!!! failure "Example messages" 583 584 ``` 585 [json.exception.type_error.304] cannot use at() with string 586 ``` 587 ``` 588 [json.exception.type_error.304] cannot use at() with number 589 ``` 590 591### json.exception.type_error.305 592 593The `operator[]` member functions can only be executed for certain JSON types. 594 595!!! failure "Example messages" 596 597 ``` 598 [json.exception.type_error.305] cannot use operator[] with a string argument with array 599 ``` 600 ``` 601 [json.exception.type_error.305] cannot use operator[] with a numeric argument with object 602 ``` 603 604### json.exception.type_error.306 605 606The `value()` member functions can only be executed for certain JSON types. 607 608!!! failure "Example message" 609 610 ``` 611 [json.exception.type_error.306] cannot use value() with number 612 ``` 613 614### json.exception.type_error.307 615 616The [`erase()`](../api/basic_json/erase.md) member functions can only be executed for certain JSON types. 617 618!!! failure "Example message" 619 620 ``` 621 [json.exception.type_error.307] cannot use erase() with string 622 ``` 623 624### json.exception.type_error.308 625 626The `push_back()` and `operator+=` member functions can only be executed for certain JSON types. 627 628!!! failure "Example message" 629 630 ``` 631 [json.exception.type_error.308] cannot use push_back() with string 632 ``` 633 634### json.exception.type_error.309 635 636The `insert()` member functions can only be executed for certain JSON types. 637 638!!! failure "Example messages" 639 640 ``` 641 [json.exception.type_error.309] cannot use insert() with array 642 ``` 643 ``` 644 [json.exception.type_error.309] cannot use insert() with number 645 ``` 646 647### json.exception.type_error.310 648 649The `swap()` member functions can only be executed for certain JSON types. 650 651!!! failure "Example message" 652 653 ``` 654 [json.exception.type_error.310] cannot use swap() with number 655 ``` 656 657### json.exception.type_error.311 658 659The `emplace()` and `emplace_back()` member functions can only be executed for certain JSON types. 660 661!!! failure "Example messages" 662 663 ``` 664 [json.exception.type_error.311] cannot use emplace() with number 665 ``` 666 ``` 667 [json.exception.type_error.311] cannot use emplace_back() with number 668 ``` 669 670### json.exception.type_error.312 671 672The `update()` member functions can only be executed for certain JSON types. 673 674!!! failure "Example message" 675 676 ``` 677 [json.exception.type_error.312] cannot use update() with array 678 ``` 679 680### json.exception.type_error.313 681 682The `unflatten` function converts an object whose keys are JSON Pointers back into an arbitrary nested JSON value. The JSON Pointers must not overlap, because then the resulting value would not be well-defined. 683 684!!! failure "Example message" 685 686 ``` 687 [json.exception.type_error.313] invalid value to unflatten 688 ``` 689 690### json.exception.type_error.314 691 692The `unflatten` function only works for an object whose keys are JSON Pointers. 693 694!!! failure "Example message" 695 696 Calling `unflatten()` on an array `#!json [1,2,3]`: 697 698 ``` 699 [json.exception.type_error.314] only objects can be unflattened 700 ``` 701 702### json.exception.type_error.315 703 704The `unflatten()` function only works for an object whose keys are JSON Pointers and whose values are primitive. 705 706!!! failure "Example message" 707 708 Calling `unflatten()` on an object `#!json {"/1", [1,2,3]}`: 709 710 ``` 711 [json.exception.type_error.315] values in object must be primitive 712 ``` 713 714### json.exception.type_error.316 715 716The `dump()` function only works with UTF-8 encoded strings; that is, if you assign a `std::string` to a JSON value, make sure it is UTF-8 encoded. 717 718!!! failure "Example message" 719 720 Calling `dump()` on a JSON value containing an ISO 8859-1 encoded string: 721 ``` 722 [json.exception.type_error.316] invalid UTF-8 byte at index 15: 0x6F 723 ``` 724 725!!! tip 726 727 - Store the source file with UTF-8 encoding. 728 - Pass an error handler as last parameter to the `dump()` function to avoid this exception: 729 - `json::error_handler_t::replace` will replace invalid bytes sequences with `U+FFFD` 730 - `json::error_handler_t::ignore` will silently ignore invalid byte sequences 731 732### json.exception.type_error.317 733 734The dynamic type of the object cannot be represented in the requested serialization format (e.g. a raw `true` or `null` JSON object cannot be serialized to BSON) 735 736!!! failure "Example messages" 737 738 Serializing `#!json null` to BSON: 739 ``` 740 [json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is null 741 ``` 742 Serializing `#!json [1,2,3]` to BSON: 743 ``` 744 [json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is array 745 ``` 746 747!!! tip 748 749 Encapsulate the JSON value in an object. That is, instead of serializing `#!json true`, serialize `#!json {"value": true}` 750 751## Out of range 752 753This 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. 754 755Exceptions have ids 4xx. 756 757??? example 758 759 The following code shows how an `out_of_range` exception can be caught. 760 761 ```cpp 762 --8<-- "examples/out_of_range.cpp" 763 ``` 764 765 Output: 766 767 ``` 768 --8<-- "examples/out_of_range.output" 769 ``` 770 771### json.exception.out_of_range.401 772 773The provided array index `i` is larger than `size-1`. 774 775!!! failure "Example message" 776 777 ``` 778 array index 3 is out of range 779 ``` 780 781### json.exception.out_of_range.402 782 783The 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. 784 785!!! failure "Example message" 786 787 ``` 788 array index '-' (3) is out of range 789 ``` 790 791### json.exception.out_of_range.403 792 793The provided key was not found in the JSON object. 794 795!!! failure "Example message" 796 797 ``` 798 key 'foo' not found 799 ``` 800 801### json.exception.out_of_range.404 802 803A reference token in a JSON Pointer could not be resolved. 804 805!!! failure "Example message" 806 807 ``` 808 unresolved reference token 'foo' 809 ``` 810 811### json.exception.out_of_range.405 812 813The JSON Patch operations 'remove' and 'add' can not be applied to the root element of the JSON value. 814 815!!! failure "Example message" 816 817 ``` 818 JSON pointer has no parent 819 ``` 820 821### json.exception.out_of_range.406 822 823A parsed number could not be stored as without changing it to NaN or INF. 824 825!!! failure "Example message" 826 827 ``` 828 number overflow parsing '10E1000' 829 ``` 830 831### json.exception.out_of_range.407 832 833UBJSON and BSON only support integer numbers up to 9223372036854775807. 834 835!!! failure "Example message" 836 837 ``` 838 number overflow serializing '9223372036854775808' 839 ``` 840 841!!! note 842 843 Since version 3.9.0, integer numbers beyond int64 are serialized as high-precision UBJSON numbers, and this exception does not further occur. 844 845### json.exception.out_of_range.408 846 847The size (following `#`) of an UBJSON array or object exceeds the maximal capacity. 848 849!!! failure "Example message" 850 851 ``` 852 excessive array size: 8658170730974374167 853 ``` 854 855### json.exception.out_of_range.409 856 857Key identifiers to be serialized to BSON cannot contain code point U+0000, since the key is stored as zero-terminated c-string. 858 859!!! failure "Example message" 860 861 ``` 862 BSON key cannot contain code point U+0000 (at byte 2) 863 ``` 864 865## Further exceptions 866 867This exception is thrown in case of errors that cannot be classified with the 868other exception types. 869 870Exceptions have ids 5xx. 871 872??? example 873 874 The following code shows how an `other_error` exception can be caught. 875 876 ```cpp 877 --8<-- "examples/other_error.cpp" 878 ``` 879 880 Output: 881 882 ``` 883 --8<-- "examples/other_error.output" 884 ``` 885 886### json.exception.other_error.501 887 888A JSON Patch operation 'test' failed. The unsuccessful operation is also printed. 889 890!!! failure "Example message" 891 892 Executing `#!json {"op":"test", "path":"/baz", "value":"bar"}` on `#!json {"baz": "qux"}`: 893 894 ``` 895 [json.exception.other_error.501] unsuccessful: {"op":"test","path":"/baz","value":"bar"} 896 ``` 897