Menu

syntax error on valid function try-catch block

Martin
2020-10-12
2021-03-29
  • Martin

    Martin - 2020-10-12

    Hi,

    the latest cppcheck exits with a syntax error on this valid code:

    int main(int argc, char **argv) try
    {
    return 0;
    }
    catch (Error &e)
    {
    return 1;
    }

    error: syntax error: keyword 'try' is not allowed in global scope [syntaxError]
    int main(int argc, char **argv) try

    See f.e.: https://stackoverflow.com/questions/8405390/function-try-catch-syntax-and-main
    Could you please check?

    Thanks,
    Martin

     
  • Daniel Marjamäki

    A stylistic question. Why not add extra semicolons to make it more readable?

     
  • Daniel Marjamäki

    I don't think I've ever seen such syntax in real code. Is this from real production code?

     
  • Martin

    Martin - 2020-10-19

    Yes, we have examples of that in our production code, and it's a valid C++ construct:
    https://en.cppreference.com/w/cpp/language/function-try-block

     
  • Daniel Marjamäki

    Yes it is valid. Cppcheck should handle it. I think it looks weird but feel free to use whatever constructs you like in your code. If you have time to fix this in Cppcheck, feel free to do it. I would add a pass in our tokenizer that inserts extra { and } tokens, that should be relatively simple and quick.

     
  • Nir Azkiel

    Nir Azkiel - 2021-03-22

    Hi,

    Is there any workaround regarding this issue?
    The error code is syntaxError, can I suppress syntaxError using the command line parameters?

    syntaxError is handled by the compiler so I think it's OK to suppress it until it will be fixed in cppcheck.

     
  • Daniel Marjamäki

    to suppress it:

    danielm@debian:~/cppcheck$ ./cppcheck 9728.cpp 
    Checking 9728.cpp ...
    9728.cpp:2:1: error: syntax error: keyword 'try' is not allowed in global scope [syntaxError]
    try {} catch (...) {}
    ^
    danielm@debian:~/cppcheck$ ./cppcheck --suppress=syntaxError 9728.cpp 
    Checking 9728.cpp ...
    danielm@debian:~/cppcheck$ 
    
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.