template<template< typename U, typename V, typename... Args > class ObjectType = std::map, template< typename U, typename... Args > 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 U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
This class is an extension of std::exception
objects with a member id for exception ids. 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.
Subclasses:
- 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
- out_of_range for exceptions indicating access out of the defined range
- other_error for exceptions indicating other library errors
- Example^^ The following code shows how arbitrary library exceptions can be
- caught. ^^
2 #include <nlohmann/json.hpp> 11 json j = {{
"foo",
"bar"}};
12 json k = j.at(
"non-existing");
17 std::cout <<
"message: " << e.what() <<
'\n' 18 <<
"exception id: " << e.id << std::endl;
basic_json<> json
default JSON class
detail::exception exception
general exception of the basic_json class
Output (play with this example online):^^ message: [json.exception.out_of_range.403] key 'non-existing' not found
exception id: 403
^^ The example code above can be translated withg++ -std=c++11 -Isingle_include doc/examples/exception.cpp -o exception
- Since
- version 3.0.0
Definition at line 12614 of file json.hpp.