Re: Exception Misconceptions: Exceptions are for unrecoverable
errors.
On Dec 22, 3:15 pm, "dragan" <spambus...@prodigy.net> wrote:
Saying "Exceptions are for unrecoverable errors" seems to
imply that they are to be used only or mostly in such
situations.
Maybe, maybe not. It probably depends on what whoever is
saying it means by "unrecoverable". (But I've never heard
anyone say it, so I don't know the context.)
Whereas the exception machinery in C++ was developed primarily
to handle and RECOVER from more errors more elegantly than was
possible without exceptions, the statement is highly suspect.
It depends on the error. At the lowest level, where a simple
retry suffices, exceptions complicate error handling.
Those "unrecoverable errors" are probably the things that
assertion checking weeds out during development time rather
than being an application for exception machinery.
If that's what is meant by "unrecoverable", then exceptions are
certainly not for unrecoverable errors. In such cases, it's
important (in most applications---there are exceptions) to kill
the process as soon as possible, without any stack walkback.
"Unrecoverable error": throw up a dialog for the developer and
exit (or crash to keep the call stack in view in the
debugger).
Exactly. And that's not what exceptions do.
Exceptions, are more appropriately used where there is a
likelihood of recovering from the error condition rather than
being just for "unrecoverable errors".
Again, it depends on what you mean by "recovering". If you just
have to try again (e.g. user input error), then a simple while
loop is a lot simpler than an exception. Exceptions are useful
when you can't really do anything about the error locally.
Which often means that "recovery" is more or less impossible,
since once you've left "locally", you've lost all of the
necessary context to retry.
But maybe you mean something different by "recovering"?
Futher discussion of the application of exceptions is
appreciated. Keep in mind that while there are SOME general
vague "principles" of the application of exceptions, mostly
the scenarios need to be defined for fruitful discussion.
Please keep the examples simple but realistic and note the
assumptions you make.
Concrete example:
//! \pre
//! No current output set up...
void
SomeClass::setupOutput()
{
assert(! myOutput.is_open());
std::string filename = getFilename(); // Dialog with user...
myOutput.open(filename.c_str());
while (! myOutput.is_open()) {
reportErrorToUser();
filename = getFilename();
myOutput.open(filename.c_str());
}
}
Use of exceptions here would only make the code more
complicated. (Try writing it in Java sometime:-). Where
failure to open a file is reported via an exception.)
--
James Kanze