try catch nlohmann::detail::parse_error
当出现错误信息时 throwing an instance of 'nlohmann::detail::type_error' 解决办法如下:

直接上代码了。
第一种方法:

try {
std::string testStr = "const.net.cn";
mJsonObj = nlohmann::json::parse(testStr);
} catch (nlohmann::detail::parse_error &error) {
std::cout << "parse_error" << std::endl;
} catch (nlohmann::detail::exception &error) {
std::cout << "exception" << std::endl;
} catch (std::overflow_error &error) {
std::cout << "overflow_error" << std::endl;
}

第二种方法:

std::string testStr = "const.net.cn";
try {
nlohmann::json::parse(testStr.c_str());
} catch (std::exception &exception) {
std::cout << "exception" << std::endl;
} catch (std::runtime_error &error) {
std::cout << "error" << std::endl;
} catch (...) {
std::cout << "any" << std::endl;
}
本文链接地址:https://const.net.cn/535.html

标签: none

添加新评论