Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 768
Jennifer M. Avatar asked Jan 17 '19 19:01

Jennifer M.


People also ask

Does C language have try catch?

Try-Catch mechanisms are common in many programming languages such as Python, C++, and JavaScript.

Can we handle exception without try catch?

throws: The throws keyword is used for exception handling without try & catch block. It specifies the exceptions that a method can throw to the caller and does not handle itself.

Is catch block mandatory with try in C#?

It is not mandatory. The catch statement is the exception handler in the Try Catch statements. A try block can have multiple catch blocks. But it must be declared the Exception class is a final one.

Does C language have exceptions?

C does not have exception handling facilities. Errors are handled by examining the value returned by each function and signals (conditions reported to the program) are handled by using library functions.


1 Answers

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.

like image 165
Algirdas Preidžius Avatar answered Oct 13 '22 00:10

Algirdas Preidžius