🔨 added user-defined exceptions 306

This commit is contained in:
Niels Lohmann 2017-03-05 19:15:56 +01:00
parent aa842b4a27
commit bb740c43fb
No known key found for this signature in database
GPG key ID: 7F3CEA63AE251B69
3 changed files with 87 additions and 64 deletions

View file

@ -4176,7 +4176,7 @@ class basic_json
@return copy of the element at key @a key or @a default_value if @a key @return copy of the element at key @a key or @a default_value if @a key
is not found is not found
@throw std::domain_error if JSON is not an object; example: `"cannot use @throw type_error.306 if JSON is not an object; example: `"cannot use
value() with null"` value() with null"`
@complexity Logarithmic in the size of the container. @complexity Logarithmic in the size of the container.
@ -4209,7 +4209,7 @@ class basic_json
} }
else else
{ {
JSON_THROW(std::domain_error("cannot use value() with " + type_name())); JSON_THROW(type_error(306, "cannot use value() with " + type_name()));
} }
} }
@ -4251,7 +4251,7 @@ class basic_json
@return copy of the element at key @a key or @a default_value if @a key @return copy of the element at key @a key or @a default_value if @a key
is not found is not found
@throw std::domain_error if JSON is not an object; example: `"cannot use @throw type_error.306 if JSON is not an object; example: `"cannot use
value() with null"` value() with null"`
@complexity Logarithmic in the size of the container. @complexity Logarithmic in the size of the container.
@ -4281,7 +4281,7 @@ class basic_json
} }
} }
JSON_THROW(std::domain_error("cannot use value() with " + type_name())); JSON_THROW(type_error(306, "cannot use value() with " + type_name()));
} }
/*! /*!

View file

@ -4176,7 +4176,7 @@ class basic_json
@return copy of the element at key @a key or @a default_value if @a key @return copy of the element at key @a key or @a default_value if @a key
is not found is not found
@throw std::domain_error if JSON is not an object; example: `"cannot use @throw type_error.306 if JSON is not an object; example: `"cannot use
value() with null"` value() with null"`
@complexity Logarithmic in the size of the container. @complexity Logarithmic in the size of the container.
@ -4209,7 +4209,7 @@ class basic_json
} }
else else
{ {
JSON_THROW(std::domain_error("cannot use value() with " + type_name())); JSON_THROW(type_error(306, "cannot use value() with " + type_name()));
} }
} }
@ -4251,7 +4251,7 @@ class basic_json
@return copy of the element at key @a key or @a default_value if @a key @return copy of the element at key @a key or @a default_value if @a key
is not found is not found
@throw std::domain_error if JSON is not an object; example: `"cannot use @throw type_error.306 if JSON is not an object; example: `"cannot use
value() with null"` value() with null"`
@complexity Logarithmic in the size of the container. @complexity Logarithmic in the size of the container.
@ -4281,7 +4281,7 @@ class basic_json
} }
} }
JSON_THROW(std::domain_error("cannot use value() with " + type_name())); JSON_THROW(type_error(306, "cannot use value() with " + type_name()));
} }
/*! /*!

View file

@ -200,70 +200,84 @@ TEST_CASE("element access 2")
{ {
json j_nonobject(json::value_t::null); json j_nonobject(json::value_t::null);
const json j_nonobject_const(j_nonobject); const json j_nonobject_const(j_nonobject);
CHECK_THROWS_AS(j_nonobject.value("foo", 1), std::domain_error); CHECK_THROWS_AS(j_nonobject.value("foo", 1), json::type_error);
CHECK_THROWS_AS(j_nonobject_const.value("foo", 1), std::domain_error); CHECK_THROWS_AS(j_nonobject_const.value("foo", 1), json::type_error);
CHECK_THROWS_WITH(j_nonobject.value("foo", 1), "cannot use value() with null"); CHECK_THROWS_WITH(j_nonobject.value("foo", 1),
CHECK_THROWS_WITH(j_nonobject_const.value("foo", 1), "cannot use value() with null"); "[json.exception.type_error.306] cannot use value() with null");
CHECK_THROWS_WITH(j_nonobject_const.value("foo", 1),
"[json.exception.type_error.306] cannot use value() with null");
} }
SECTION("boolean") SECTION("boolean")
{ {
json j_nonobject(json::value_t::boolean); json j_nonobject(json::value_t::boolean);
const json j_nonobject_const(j_nonobject); const json j_nonobject_const(j_nonobject);
CHECK_THROWS_AS(j_nonobject.value("foo", 1), std::domain_error); CHECK_THROWS_AS(j_nonobject.value("foo", 1), json::type_error);
CHECK_THROWS_AS(j_nonobject_const.value("foo", 1), std::domain_error); CHECK_THROWS_AS(j_nonobject_const.value("foo", 1), json::type_error);
CHECK_THROWS_WITH(j_nonobject.value("foo", 1), "cannot use value() with boolean"); CHECK_THROWS_WITH(j_nonobject.value("foo", 1),
CHECK_THROWS_WITH(j_nonobject_const.value("foo", 1), "cannot use value() with boolean"); "[json.exception.type_error.306] cannot use value() with boolean");
CHECK_THROWS_WITH(j_nonobject_const.value("foo", 1),
"[json.exception.type_error.306] cannot use value() with boolean");
} }
SECTION("string") SECTION("string")
{ {
json j_nonobject(json::value_t::string); json j_nonobject(json::value_t::string);
const json j_nonobject_const(j_nonobject); const json j_nonobject_const(j_nonobject);
CHECK_THROWS_AS(j_nonobject.value("foo", 1), std::domain_error); CHECK_THROWS_AS(j_nonobject.value("foo", 1), json::type_error);
CHECK_THROWS_AS(j_nonobject_const.value("foo", 1), std::domain_error); CHECK_THROWS_AS(j_nonobject_const.value("foo", 1), json::type_error);
CHECK_THROWS_WITH(j_nonobject.value("foo", 1), "cannot use value() with string"); CHECK_THROWS_WITH(j_nonobject.value("foo", 1),
CHECK_THROWS_WITH(j_nonobject_const.value("foo", 1), "cannot use value() with string"); "[json.exception.type_error.306] cannot use value() with string");
CHECK_THROWS_WITH(j_nonobject_const.value("foo", 1),
"[json.exception.type_error.306] cannot use value() with string");
} }
SECTION("array") SECTION("array")
{ {
json j_nonobject(json::value_t::array); json j_nonobject(json::value_t::array);
const json j_nonobject_const(j_nonobject); const json j_nonobject_const(j_nonobject);
CHECK_THROWS_AS(j_nonobject.value("foo", 1), std::domain_error); CHECK_THROWS_AS(j_nonobject.value("foo", 1), json::type_error);
CHECK_THROWS_AS(j_nonobject_const.value("foo", 1), std::domain_error); CHECK_THROWS_AS(j_nonobject_const.value("foo", 1), json::type_error);
CHECK_THROWS_WITH(j_nonobject.value("foo", 1), "cannot use value() with array"); CHECK_THROWS_WITH(j_nonobject.value("foo", 1),
CHECK_THROWS_WITH(j_nonobject_const.value("foo", 1), "cannot use value() with array"); "[json.exception.type_error.306] cannot use value() with array");
CHECK_THROWS_WITH(j_nonobject_const.value("foo", 1),
"[json.exception.type_error.306] cannot use value() with array");
} }
SECTION("number (integer)") SECTION("number (integer)")
{ {
json j_nonobject(json::value_t::number_integer); json j_nonobject(json::value_t::number_integer);
const json j_nonobject_const(j_nonobject); const json j_nonobject_const(j_nonobject);
CHECK_THROWS_AS(j_nonobject.value("foo", 1), std::domain_error); CHECK_THROWS_AS(j_nonobject.value("foo", 1), json::type_error);
CHECK_THROWS_AS(j_nonobject_const.value("foo", 1), std::domain_error); CHECK_THROWS_AS(j_nonobject_const.value("foo", 1), json::type_error);
CHECK_THROWS_WITH(j_nonobject.value("foo", 1), "cannot use value() with number"); CHECK_THROWS_WITH(j_nonobject.value("foo", 1),
CHECK_THROWS_WITH(j_nonobject_const.value("foo", 1), "cannot use value() with number"); "[json.exception.type_error.306] cannot use value() with number");
CHECK_THROWS_WITH(j_nonobject_const.value("foo", 1),
"[json.exception.type_error.306] cannot use value() with number");
} }
SECTION("number (unsigned)") SECTION("number (unsigned)")
{ {
json j_nonobject(json::value_t::number_unsigned); json j_nonobject(json::value_t::number_unsigned);
const json j_nonobject_const(j_nonobject); const json j_nonobject_const(j_nonobject);
CHECK_THROWS_AS(j_nonobject.value("foo", 1), std::domain_error); CHECK_THROWS_AS(j_nonobject.value("foo", 1), json::type_error);
CHECK_THROWS_AS(j_nonobject_const.value("foo", 1), std::domain_error); CHECK_THROWS_AS(j_nonobject_const.value("foo", 1), json::type_error);
CHECK_THROWS_WITH(j_nonobject.value("foo", 1), "cannot use value() with number"); CHECK_THROWS_WITH(j_nonobject.value("foo", 1),
CHECK_THROWS_WITH(j_nonobject_const.value("foo", 1), "cannot use value() with number"); "[json.exception.type_error.306] cannot use value() with number");
CHECK_THROWS_WITH(j_nonobject_const.value("foo", 1),
"[json.exception.type_error.306] cannot use value() with number");
} }
SECTION("number (floating-point)") SECTION("number (floating-point)")
{ {
json j_nonobject(json::value_t::number_float); json j_nonobject(json::value_t::number_float);
const json j_nonobject_const(j_nonobject); const json j_nonobject_const(j_nonobject);
CHECK_THROWS_AS(j_nonobject.value("foo", 1), std::domain_error); CHECK_THROWS_AS(j_nonobject.value("foo", 1), json::type_error);
CHECK_THROWS_AS(j_nonobject_const.value("foo", 1), std::domain_error); CHECK_THROWS_AS(j_nonobject_const.value("foo", 1), json::type_error);
CHECK_THROWS_WITH(j_nonobject.value("foo", 1), "cannot use value() with number"); CHECK_THROWS_WITH(j_nonobject.value("foo", 1),
CHECK_THROWS_WITH(j_nonobject_const.value("foo", 1), "cannot use value() with number"); "[json.exception.type_error.306] cannot use value() with number");
CHECK_THROWS_WITH(j_nonobject_const.value("foo", 1),
"[json.exception.type_error.306] cannot use value() with number");
} }
} }
} }
@ -304,75 +318,84 @@ TEST_CASE("element access 2")
{ {
json j_nonobject(json::value_t::null); json j_nonobject(json::value_t::null);
const json j_nonobject_const(j_nonobject); const json j_nonobject_const(j_nonobject);
CHECK_THROWS_AS(j_nonobject.value("/foo"_json_pointer, 1), std::domain_error); CHECK_THROWS_AS(j_nonobject.value("/foo"_json_pointer, 1), json::type_error);
CHECK_THROWS_AS(j_nonobject_const.value("/foo"_json_pointer, 1), std::domain_error); CHECK_THROWS_AS(j_nonobject_const.value("/foo"_json_pointer, 1), json::type_error);
CHECK_THROWS_WITH(j_nonobject.value("/foo"_json_pointer, 1), "cannot use value() with null"); CHECK_THROWS_WITH(j_nonobject.value("/foo"_json_pointer, 1),
CHECK_THROWS_WITH(j_nonobject_const.value("/foo"_json_pointer, 1), "cannot use value() with null"); "[json.exception.type_error.306] cannot use value() with null");
CHECK_THROWS_WITH(j_nonobject_const.value("/foo"_json_pointer, 1),
"[json.exception.type_error.306] cannot use value() with null");
} }
SECTION("boolean") SECTION("boolean")
{ {
json j_nonobject(json::value_t::boolean); json j_nonobject(json::value_t::boolean);
const json j_nonobject_const(j_nonobject); const json j_nonobject_const(j_nonobject);
CHECK_THROWS_AS(j_nonobject.value("/foo"_json_pointer, 1), std::domain_error); CHECK_THROWS_AS(j_nonobject.value("/foo"_json_pointer, 1), json::type_error);
CHECK_THROWS_AS(j_nonobject_const.value("/foo"_json_pointer, 1), std::domain_error); CHECK_THROWS_AS(j_nonobject_const.value("/foo"_json_pointer, 1), json::type_error);
CHECK_THROWS_WITH(j_nonobject.value("/foo"_json_pointer, 1), "cannot use value() with boolean"); CHECK_THROWS_WITH(j_nonobject.value("/foo"_json_pointer, 1),
"[json.exception.type_error.306] cannot use value() with boolean");
CHECK_THROWS_WITH(j_nonobject_const.value("/foo"_json_pointer, 1), CHECK_THROWS_WITH(j_nonobject_const.value("/foo"_json_pointer, 1),
"cannot use value() with boolean"); "[json.exception.type_error.306] cannot use value() with boolean");
} }
SECTION("string") SECTION("string")
{ {
json j_nonobject(json::value_t::string); json j_nonobject(json::value_t::string);
const json j_nonobject_const(j_nonobject); const json j_nonobject_const(j_nonobject);
CHECK_THROWS_AS(j_nonobject.value("/foo"_json_pointer, 1), std::domain_error); CHECK_THROWS_AS(j_nonobject.value("/foo"_json_pointer, 1), json::type_error);
CHECK_THROWS_AS(j_nonobject_const.value("/foo"_json_pointer, 1), std::domain_error); CHECK_THROWS_AS(j_nonobject_const.value("/foo"_json_pointer, 1), json::type_error);
CHECK_THROWS_WITH(j_nonobject.value("/foo"_json_pointer, 1), "cannot use value() with string"); CHECK_THROWS_WITH(j_nonobject.value("/foo"_json_pointer, 1),
"[json.exception.type_error.306] cannot use value() with string");
CHECK_THROWS_WITH(j_nonobject_const.value("/foo"_json_pointer, 1), CHECK_THROWS_WITH(j_nonobject_const.value("/foo"_json_pointer, 1),
"cannot use value() with string"); "[json.exception.type_error.306] cannot use value() with string");
} }
SECTION("array") SECTION("array")
{ {
json j_nonobject(json::value_t::array); json j_nonobject(json::value_t::array);
const json j_nonobject_const(j_nonobject); const json j_nonobject_const(j_nonobject);
CHECK_THROWS_AS(j_nonobject.value("/foo"_json_pointer, 1), std::domain_error); CHECK_THROWS_AS(j_nonobject.value("/foo"_json_pointer, 1), json::type_error);
CHECK_THROWS_AS(j_nonobject_const.value("/foo"_json_pointer, 1), std::domain_error); CHECK_THROWS_AS(j_nonobject_const.value("/foo"_json_pointer, 1), json::type_error);
CHECK_THROWS_WITH(j_nonobject.value("/foo"_json_pointer, 1), "cannot use value() with array"); CHECK_THROWS_WITH(j_nonobject.value("/foo"_json_pointer, 1),
CHECK_THROWS_WITH(j_nonobject_const.value("/foo"_json_pointer, 1), "cannot use value() with array"); "[json.exception.type_error.306] cannot use value() with array");
CHECK_THROWS_WITH(j_nonobject_const.value("/foo"_json_pointer, 1),
"[json.exception.type_error.306] cannot use value() with array");
} }
SECTION("number (integer)") SECTION("number (integer)")
{ {
json j_nonobject(json::value_t::number_integer); json j_nonobject(json::value_t::number_integer);
const json j_nonobject_const(j_nonobject); const json j_nonobject_const(j_nonobject);
CHECK_THROWS_AS(j_nonobject.value("/foo"_json_pointer, 1), std::domain_error); CHECK_THROWS_AS(j_nonobject.value("/foo"_json_pointer, 1), json::type_error);
CHECK_THROWS_AS(j_nonobject_const.value("/foo"_json_pointer, 1), std::domain_error); CHECK_THROWS_AS(j_nonobject_const.value("/foo"_json_pointer, 1), json::type_error);
CHECK_THROWS_WITH(j_nonobject.value("/foo"_json_pointer, 1), "cannot use value() with number"); CHECK_THROWS_WITH(j_nonobject.value("/foo"_json_pointer, 1),
"[json.exception.type_error.306] cannot use value() with number");
CHECK_THROWS_WITH(j_nonobject_const.value("/foo"_json_pointer, 1), CHECK_THROWS_WITH(j_nonobject_const.value("/foo"_json_pointer, 1),
"cannot use value() with number"); "[json.exception.type_error.306] cannot use value() with number");
} }
SECTION("number (unsigned)") SECTION("number (unsigned)")
{ {
json j_nonobject(json::value_t::number_unsigned); json j_nonobject(json::value_t::number_unsigned);
const json j_nonobject_const(j_nonobject); const json j_nonobject_const(j_nonobject);
CHECK_THROWS_AS(j_nonobject.value("/foo"_json_pointer, 1), std::domain_error); CHECK_THROWS_AS(j_nonobject.value("/foo"_json_pointer, 1), json::type_error);
CHECK_THROWS_AS(j_nonobject_const.value("/foo"_json_pointer, 1), std::domain_error); CHECK_THROWS_AS(j_nonobject_const.value("/foo"_json_pointer, 1), json::type_error);
CHECK_THROWS_WITH(j_nonobject.value("/foo"_json_pointer, 1), "cannot use value() with number"); CHECK_THROWS_WITH(j_nonobject.value("/foo"_json_pointer, 1),
"[json.exception.type_error.306] cannot use value() with number");
CHECK_THROWS_WITH(j_nonobject_const.value("/foo"_json_pointer, 1), CHECK_THROWS_WITH(j_nonobject_const.value("/foo"_json_pointer, 1),
"cannot use value() with number"); "[json.exception.type_error.306] cannot use value() with number");
} }
SECTION("number (floating-point)") SECTION("number (floating-point)")
{ {
json j_nonobject(json::value_t::number_float); json j_nonobject(json::value_t::number_float);
const json j_nonobject_const(j_nonobject); const json j_nonobject_const(j_nonobject);
CHECK_THROWS_AS(j_nonobject.value("/foo"_json_pointer, 1), std::domain_error); CHECK_THROWS_AS(j_nonobject.value("/foo"_json_pointer, 1), json::type_error);
CHECK_THROWS_AS(j_nonobject_const.value("/foo"_json_pointer, 1), std::domain_error); CHECK_THROWS_AS(j_nonobject_const.value("/foo"_json_pointer, 1), json::type_error);
CHECK_THROWS_WITH(j_nonobject.value("/foo"_json_pointer, 1), "cannot use value() with number"); CHECK_THROWS_WITH(j_nonobject.value("/foo"_json_pointer, 1),
"[json.exception.type_error.306] cannot use value() with number");
CHECK_THROWS_WITH(j_nonobject_const.value("/foo"_json_pointer, 1), CHECK_THROWS_WITH(j_nonobject_const.value("/foo"_json_pointer, 1),
"cannot use value() with number"); "[json.exception.type_error.306] cannot use value() with number");
} }
} }
} }