J
junw2000
Is C++ Exception handling useful? think it is too complicated. What
kinds of project need to use it? Thanks.
kinds of project need to use it? Thanks.
Is C++ Exception handling useful? think it is too complicated. What
kinds of project need to use it? Thanks.
Is C++ Exception handling useful? think it is too complicated.
What kinds of project need to use it?
Is C++ Exception handling useful? think it is too complicated. What
kinds of project need to use it? Thanks.
Michael said:The C++ Exception Model:
* destructors are invoked for all live objects as the stack of function
calls "unwinds."
* exception specifications specify what type of exception(s) a function
will throw.
Unfortunately, that's not quite what exception specifications do, since
there's no way in general for the exception specification to be
enforced by the compiler.
They are generally regarded as the one part
of C++ exception handling that is largely useless.
Roland said:Unlike Java, C++ exception specification are supposed to be enforced
by _you_, the programmer, not the compiler. You (can) guarantee that
only the specified exception(s) may be thrown from a function.
Nope, they are useful especially for high level functions and
interfaces. They tighten the 'contract' between implementor and user.
How? Since the exception specification is not enforced by the compiler,
how is it anything more than documentation of what the function is
supposed to do?
Roland said:They are enforced in the code by the implementor. That's the point.
The user can rely on the exception specification.
If your exception specification is not correct then it's a bug in your
code (that probably crashes the program).
That's much more than 'just'
documentation. BTW, if you know which exception can be thrown you can
specifiy it in code _and_ documentation. If you don't know you can do
neither.
"Too complicated" compared to what? All alternatives that can solveIs C++ Exception handling useful? think it is too complicated. What
kinds of project need to use it? Thanks.
Gavin Deane said:Unfortunately, that's not quite what exception specifications do, since
there's no way in general for the exception specification to be
enforced by the compiler. They are generally regarded as the one part
of C++ exception handling that is largely useless.
http://www.gotw.ca/gotw/082.htm
http://www.boost.org/more/lib_guide.htm#Exception-specification
If your exception specification is not correct then it's a bug in your
code (that probably crashes the program). That's much more than 'just'
documentation. BTW, if you know which exception can be thrown you can
specifiy it in code _and_ documentation. If you don't know you can do
neither.
But int the presence of function objects, call back functions,
polymorphism or templates, it's almost impossible to make your
exception specifications correct, and therefore almost guaranteeing
that your program *will* crash.
Roland said:You mean you cannot safely write a function that complies to an
exception specification?
void foo() throw (MyException) {
try {
// use function objects, call back functions,
// polymorphism or templates
} catch (MyException& e) {
throw;
} catch (std::exception& e) {
throw MyException (e);
} catch (...) {
throw MyException ("unknown exception in foo()");
}
}
For a generalized 'ExceptionFilter' see
http://article.gmane.org/gmane.comp.lib.boost.devel/132551
Best wishes,
Roland Pibinger
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.