try { throw 5; } catch (const int& val) { printf("%d\n", val); }
try { throw "hello_world"; } catch (const char *&val) { puts(val); }
I think it would have been much better (and simpler for implementers!) if they had made std::exception a more fundamental part of the language and required that throw be used on a descendant of it. As it stands if you want to be sure to catch any failure from some external code you end up doing: try {
something();
} catch (const std::exception& ex) {
fprintf(stderr, "ERROR: %s\n", ex.what());
} catch (...) {
fputs("UNKNOWN ERROR\n", stderr);
}
My personal opinion is that it's easier to deal with C++ exceptions (warts and all) than to try to avoid them. Just about any method that mutates a STL type can have a heap allocation fail and the only reasonable way to report that is via "throw std::bad_alloc();". Just make sure that your code always follows the principals of RAII ( http://en.wikipedia.org/wiki/RAII ) and you'll be fine.It's as if every time a machine broke down in a factory, the workmen would have to wheel the machine into the manager's office, shove it on his desk and shout, "there, there's your problem!"... and if the manager didn't grab the machine, it would fly through his back window...
Combining those two, you have code that wasn't written exception defensively having unexpected exit paths due to exceptions and now you've got bugs.
It sounds like they would've included exceptions if they didn't have a legacy codebase. I'm sure that would've added a decent amount to the doc.
As they say that the end "Use common sense and BE CONSISTENT." http://google-styleguide.googlecode.com/svn/trunk/cppguide.x... If you are modifying a project that uses exceptions, then use exceptions. If you then modify a different project that uses error codes instead, then use error codes. Be consistent above all else.
1) It means that you have to check the return value of every call. Miss one, and errors get ignored, which can result memory corruption or worse.
2) Checking return codes after every function call results in terribly verbose code/ Whereas exception-based error handling, when used in the right way, only needs catch() statements in places where the error is actually handled.
2) Every function has to return a status code. This hijacks the return value, meaning that you cannot simply return a value anymore, you always need to use output parameters.