diff --git a/README.md b/README.md index 9f972c211..e34afaf4d 100644 --- a/README.md +++ b/README.md @@ -1648,7 +1648,7 @@ Here is a related issue [#1924](https://github.com/nlohmann/json/issues/1924). - The code contains numerous debug **assertions** which can be switched off by defining the preprocessor macro `NDEBUG`, see the [documentation of `assert`](https://en.cppreference.com/w/cpp/error/assert). In particular, note [`operator[]`](https://nlohmann.github.io/json/api/basic_json/operator%5B%5D/) implements **unchecked access** for const objects: If the given key is not present, the behavior is undefined (think of a dereferenced null pointer) and yields an [assertion failure](https://github.com/nlohmann/json/issues/289) if assertions are switched on. If you are not sure whether an element in an object exists, use checked access with the [`at()` function](https://nlohmann.github.io/json/api/basic_json/at/). Furthermore, you can define `JSON_ASSERT(x)` to replace calls to `assert(x)`. - As the exact type of a number is not defined in the [JSON specification](https://tools.ietf.org/html/rfc8259.html), this library tries to choose the best fitting C++ number type automatically. As a result, the type `double` may be used to store numbers which may yield [**floating-point exceptions**](https://github.com/nlohmann/json/issues/181) in certain rare situations if floating-point exceptions have been unmasked in the calling code. These exceptions are not caused by the library and need to be fixed in the calling code, such as by re-masking the exceptions prior to calling library functions. - The code can be compiled without C++ **runtime type identification** features; that is, you can use the `-fno-rtti` compiler flag. -- **Exceptions** 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`. In this case, exceptions are replaced by `abort()` calls. You can further control this behavior by defining `JSON_THROW_USER` (overriding `throw`), `JSON_TRY_USER` (overriding `try`), and `JSON_CATCH_USER` (overriding `catch`). Note that `JSON_THROW_USER` should leave the current scope (e.g., by throwing or aborting), as continuing after it may yield undefined behavior. +- **Exceptions** 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`. In this case, exceptions are replaced by `abort()` calls. You can further control this behavior by defining `JSON_THROW_USER` (overriding `throw`), `JSON_TRY_USER` (overriding `try`), and `JSON_CATCH_USER` (overriding `catch`). Note that `JSON_THROW_USER` should leave the current scope (e.g., by throwing or aborting), as continuing after it may yield undefined behavior. Note 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). ## Execute unit tests diff --git a/doc/mkdocs/docs/features/macros.md b/doc/mkdocs/docs/features/macros.md index d008393f7..8b10ef1f3 100644 --- a/doc/mkdocs/docs/features/macros.md +++ b/doc/mkdocs/docs/features/macros.md @@ -32,6 +32,8 @@ When defining `JSON_NOEXCEPTION`, `#!cpp try` is replaced by `#!cpp if (true)`, The same effect is achieved by setting the compiler flag `-fno-exceptions`. +Note 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). + ## `JSON_NO_IO` When defined, headers ``, ``, ``, ``, and `` are not included and parse functions relying on these headers are excluded. This is relevant for environment where these I/O functions are disallowed for security reasons (e.g., Intel Software Guard Extensions (SGX)). diff --git a/doc/mkdocs/docs/home/exceptions.md b/doc/mkdocs/docs/home/exceptions.md index d5d07362f..a04b60f86 100644 --- a/doc/mkdocs/docs/home/exceptions.md +++ b/doc/mkdocs/docs/home/exceptions.md @@ -50,6 +50,8 @@ Note that `JSON_THROW_USER` should leave the current scope (e.g., by throwing or #include ``` +Note 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). + ### Extended diagnostic messages Exceptions 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. diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 271b8c77a..d0ae29438 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -95,8 +95,9 @@ target_compile_definitions(test-disabled_exceptions PUBLIC JSON_NOEXCEPTION) if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU") target_compile_options(test-disabled_exceptions PUBLIC -fno-exceptions) elseif (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") - target_compile_options(test-disabled_exceptions PUBLIC /EH) - target_compile_definitions(test-disabled_exceptions PUBLIC _HAS_EXCEPTIONS=0) + # disabled due to https://github.com/nlohmann/json/discussions/2824 + #target_compile_options(test-disabled_exceptions PUBLIC /EH) + #target_compile_definitions(test-disabled_exceptions PUBLIC _HAS_EXCEPTIONS=0) endif() add_executable(json_unit EXCLUDE_FROM_ALL $ ${files}) diff --git a/test/src/unit-disabled_exceptions.cpp b/test/src/unit-disabled_exceptions.cpp index 5a29958c9..6da9dcf1d 100644 --- a/test/src/unit-disabled_exceptions.cpp +++ b/test/src/unit-disabled_exceptions.cpp @@ -52,48 +52,6 @@ class sax_no_exception : public nlohmann::detail::json_sax_dom_parser std::string* sax_no_exception::error_string = nullptr; -// -#include // exception -#include // runtime_error - -// -namespace nlohmann -{ -namespace detail2 -{ - -class exception : public std::exception -{ - public: - const char* what() const noexcept override - { - return m.what(); - } - - protected: - exception(const char* what_arg) : m(what_arg) {} - - private: - std::runtime_error m; -}; - -class parse_error : public exception -{ - public: - static parse_error create(const std::string& what_arg) - { - std::string w = "[json.exception.parse_error] " + what_arg; - return parse_error(w.c_str()); - } - - private: - parse_error(const char* what_arg) : exception(what_arg) {} -}; - -} // namespace detail2 -} // namespace nlohmann -// - TEST_CASE("Tests with disabled exceptions") { SECTION("issue #2824 - encoding of json::exception::what()") @@ -102,13 +60,7 @@ TEST_CASE("Tests with disabled exceptions") sax_no_exception sax(j); CHECK (!json::sax_parse("xyz", &sax)); - //CHECK(*sax_no_exception::error_string == "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - invalid literal; last read: 'x'"); + CHECK(*sax_no_exception::error_string == "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - invalid literal; last read: 'x'"); delete sax_no_exception::error_string; // NOLINT(cppcoreguidelines-owning-memory) } - - SECTION("test") - { - auto error = nlohmann::detail2::parse_error::create("foo"); - CHECK(std::string(error.what()) == "[json.exception.parse_error] foo"); - } }