Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

try/catch block does not catch parsing error #2579

Closed
bfamzz opened this issue Jan 9, 2021 · 7 comments
Closed

try/catch block does not catch parsing error #2579

bfamzz opened this issue Jan 9, 2021 · 7 comments
Labels
solution: invalid the issue is not related to the library

Comments

@bfamzz
Copy link

bfamzz commented Jan 9, 2021

What is the issue you have?

try/catch block does not catch parsing error

Error message is:
[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - invalid literal; last read: 'A'

Please describe the steps to reproduce the issue.

  1. Sample code is provided below.

Can you provide a small but working code example?

#include<iostream>
#include <nlohmann/json.hpp>

int main()
{
    try
    {
        nlohmann::json jsonObject = nlohmann::json::parse("Alright");
        std::cout << "Parse completed!\n";
    }
    catch(const std::exception& e)
    {
        std::cerr << e.what() << '\n';
        return -1;
    }
    return 0;
}

What is the expected behavior?

Error should be caught in the try/catch block and program should continue execution.

And what is the actual behavior instead?

Program crashes while in the try block. Parsing error is not caught.

Which compiler and operating system are you using?

  • Compiler: GCC 9.3.0 && GCC 10.1.0
  • Operating system: Linux - Ubuntu 20.04

Which version of the library did you use?

  • [x] latest release version 3.9.1
@nlohmann
Copy link
Owner

I cannot reproduce this issue: https://godbolt.org/z/d396TE

@nlohmann nlohmann added the state: needs more info the author of the issue needs to provide more details label Jan 10, 2021
@nlohmann
Copy link
Owner

The libraries exceptions do inherit from std::exception. There must be something wrong in your setup I'm afraid.

@bfamzz
Copy link
Author

bfamzz commented Jan 11, 2021

Hi @nlohmann

I cannot reproduce this issue: https://godbolt.org/z/d396TE

Following the link you posted, the error message was displayed.

Kindly see screenshot below.

image

@nlohmann
Copy link
Owner

The error message is displayed, because of the line

std::cerr << e.what() << '\n';

And that line is only executed, because the exception has be caught.

@bfamzz
Copy link
Author

bfamzz commented Jan 11, 2021

Thanks @nlohmann You are right. There must be something wrong with my setup. I will close this and investigate my setup further. If I find anything, I'll update this thread with my findings. Thanks for your help.

@bfamzz bfamzz closed this as completed Jan 11, 2021
@nlohmann nlohmann added solution: invalid the issue is not related to the library and removed kind: bug state: needs more info the author of the issue needs to provide more details labels Jan 11, 2021
@xiaozhuai
Copy link

@bfamzz @nlohmann
I got the same issue.
On MacOS, it works fine
On android with NDK r15c, it cannot catch the exception, same code

    try {
        throw std::runtime_error("aaaaa");
    } catch (std::exception &e) {
        std::cout << e.what() << "\n"; // works
    }

    try {
        auto aaa = json::parse("{aaaa}");
    } catch (json::exception &e) {
        std::cout << e.what() << "\n"; // not work
    }

@nlohmann
Copy link
Owner

@xiaozhuai Can you share more information on your setup?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
solution: invalid the issue is not related to the library
Projects
None yet
Development

No branches or pull requests

3 participants