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?
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?
I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.
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.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Last edited by grumpy; 04-02-2011 at 04:20 PM.
From [except.handle]p9:
I don't make claims about the standard without first looking things up.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).
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Thanks guys. I appreciate the info.
I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.
Crashing is just one form of terminating.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
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; }
Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling