stephenberry / glaze

Extremely fast, in memory, JSON and interface library for modern C++
MIT License
1.23k stars 123 forks source link

Can't catch exceptions in get_T methods #1419

Closed divannyteoretik closed 3 weeks ago

divannyteoretik commented 3 weeks ago

I don't disable exceptions, but I can't catch exceptions of get_T methods (get_object, get_string, and so on) while I can do that with direct calls get<T>. It seems like that's because get_T are marked with noexcept in the json_t.hpp file.

    std::string number_str = R"(1)";
    glz::json_t number_json;
    glz::read_json(number_json, number_str);
    try {
        //std::cout << number_json.get_string() << std::endl; // can't catch it
        std::cout << number_json.get<std::string>() << std::endl; // catches it
    } catch (const std::exception& e) {
        std::cout << "Exception: " << e.what() << std::endl;
    }

Demo is here https://gcc.godbolt.org/z/3K3sczbEY and it can be reproduced on both gcc and clang.

Is glaze supposed to behave in this way? If so, I believe it's better to say that explicitly in docs. If not, maybe it worth removing noexcept from get_T methods.

stephenberry commented 3 weeks ago

Thanks for reporting this. This was a copy/paste error and should not be marked noexcept. I'll fix this and also remove a number of places where it is inappropriately marked for use in exception contexts. Glaze has been too aggressive with marking things noexcept in the past and we need to allow these exceptions through.

stephenberry commented 3 weeks ago

A fix has been merged in #1420