I noticed that I don't have to explicitly catch an exception thrown in the main() function. I just throw it, and what was thrown is outputted to screen, and the program is automatically terminated prematurely.
Why is this?
Printable View
I noticed that I don't have to explicitly catch an exception thrown in the main() function. I just throw it, and what was thrown is outputted to screen, and the program is automatically terminated prematurely.
Why is this?
An uncaught exception is required by the standard to result in stack unwinding (e.g. all local variables or objects are cleaned up for all call functions) and program termination.
Presumably your implementation (compiler, library, etc) also causes relevant output to occur. The standard does not require that, so such behaviour is not guaranteed with other implementations.
From [except.handle]p9:
I don't make claims about the standard without first looking things up.Quote:
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 (15.5.1).
Thanks guys. I appreciate the info.
Crashing is just one form of terminating.
All my main() functions look the same:
Then main_program takes the role of the main function.Code:int main(int argc, char** argv)
{
using namespace std;
try {
return main_program(argc,argv);
}
catch (runtime_error& e) {
cerr << RED << "Run-time error : " << e.what() << NORMAL << endl;
}
catch (bad_alloc&) {
statusFailed();
cerr << RED << "Out of memory." << NORMAL << endl;
}
catch (exception& e) {
cerr << RED << "Exception : " << e.what() << NORMAL << endl;
}
catch (...) {
cerr << RED << "Unknown error" << NORMAL << endl;
}
return 1;
}