Are C++ programs using exceptions required to have try/catch block in their main function?

  • A+

If the exception is thrown by the C++ code but is not caught, it causes SIGABRT. Some systems just print "Abort", some other systems also print the contents of e.what().

The question is: Does the C++ standard say that try/catch block is required in the main function for the program to be considered a well behaved program, or does C++ just silently rely on the system to process this?


As described in C++17 standard draft, in section 18.3.9 [except.handle]:

If no matching handler is found, the function std::terminate() is called; whether or not the stack is unwound before this call to std::terminate() is implementation-defined.

So, the behavior of such program is not considered undefined, since standard defines, that std::terminate will be called.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: