"Warning: An exception may be thrown in function 'main' which should not throw exceptions" in new VS C++ project

Hello,

I just started working with C++ and I started a new C++ console app project in Visual Studio. Right from the start I am getting the warning in the post subject about main possibly throwing an exception. At this point, main is nothing more than a call to std::cout to print "Hello world!"

I understand what the warning is trying to tell me but not sure how to get rid of it (without turning it off). Am I missing something in my main that is best practice in C++?

Thanks!

3 comments
Comment actions Permalink

Hello!

This is a clang-tidy check, you can always refer to the documentation to check what the purpose of the check is and when it triggers:

In production code, it's advised that main() should always catch all exceptions. If main() does not catch an exception, std::terminate will call std::abort by default, which means that the stack won't be unwound and destructors will not get called. See https://softwareengineering.stackexchange.com/questions/308817/should-a-c-program-catch-all-exceptions-and-prevent-exceptions-from-bubbling-u for more details.

0
Comment actions Permalink

Thanks, Igor. So if I wanted to disable this conditionally (example: display the warning when using Release configuration, but hide it when using Debug configuration) is there a way to do so? I generally don't like disabling warning/errors globally because I learn a lot from what ReSharper points out.

0
Comment actions Permalink

Sorry, there's no way to disable a check depending on which configuration is active, and in general I don't think that's the right thing to do.

If you want to hide this check for your main function, just add "// NOLINT" after its declaration (or use one of the actions from the menu above to disable the check).

0

Please sign in to leave a comment.