Re: Handling errors within templated classes
On Oct 31, 3:40 pm, "Alf P. Steinbach" <al...@start.no> wrote:
* James Kanze:
On Oct 30, 1:20 am, Kai-Uwe Bux <jkherci...@gmx.net> wrote:
Your questions/suggestions are well founded. However, I was
using a "toy" application to test alternative error-handling
strategies. One was to trap the error, display a msg and
exit the app. However, sometimes it's desirable to recover
somehow from errors so that additional errors can be exposed
in a single pass through all the calls in an app.
There are two standard solutions for this problem, depending on
the types of errors you're interested in. If the error is
something "exceptional", i.e. something which won't normally
occur (like disk full or insufficient memory), you should
definitely raise an exception.
A newbie should definitely let the program abort,
Probably. (If the newbie is working in a larger team, he should
use whatever mechanisms the team has established. But setting
up a framework to do anything but abort isn't a task for a
Running out of memory just happens to be a convenient example.
More generally, of course, you have a complete hierarchy of ways
to handle errors, from aborting, to simply noting the error and
continuing in a degraded mode (something like ostream, for
example). Which one to use will depend on the type of error and
the application constraits. There is no universally "right"
solution. Which is really what lies at the heart of the
original question: if you're writing a generic class, for use in
many different applications, what do you do? The most useful
solution is generally to use the least radical solution which
could reasonably apply. In the case of out of memory, it's hard
to imagine anything less that an exception as being acceptable,
although I agree that at least 90% of the time (newbie or not),
aborting is what is really required. But from the user's point
of view, it's easier to convert an exception into an
abort---just don't catch it:-)---than it is to convert an abort
into an exception.
in the case of memory exhaustion via a new-handler
(set_new_handler or whatever the name was), because with basic
resource depletion higher level code won't be able to do
anything and will most likely fail in turn: even the throwing
of a standard exception might fail...
Formally, just about anything might fail at any time. The
standard has a cop out saying that not enough resources is
undefined behavior. Of course, we all count on quality of
implementation as well. On the systems I use, you can catch
bad_alloc and continue (although you may have to configure the
OS correctly in order for the error to be detected). On the
systems I use, there are even ways of guaranteeing that the
stack doesn't overflow (but they're very system dependent).
It is perhaps ironic that I have argued the position of using
exception, e.g. with Dave Abrahams (who then took the position
I'm doing here, that of terminating at once). But that was in
the context of very advanced error handling, of possibly
cleaning up some things and doing a Bird Of Phoenix: error
handling for newbies and ordinary error handling is something
It all depends on the application. I write large scale server
software. My programs run on dedicated machines, with
sufficient memory. Most of the time, if I run out of memory,
the only possible reason could be a memory leak. In that case,
it's definitly best to abort and restart the application; the
situation won't get any better otherwise. But there are
exceptions: consider a protocol like LDAP, where the client can
send arbitrary "expressions", which will typically be
represented internally in the form of an expression graph.
Regardless of how much memory you have, expressions which
require more can exist. In such case, catching bad_alloc and
responding with an "insufficient resources" error is a very
viable solution. (If you've programmed correctly, the stack
walkback of the exception will ensure that all of the nodes are
If the error is something
"normal" (just about any errors in user input, for example), the
usual solution is to use some variant of the Barton and Nackman
"Fallible" idiom, perhaps extended to allow additional
information concerning the error. (There's an implementation
supporting extended error codes at my site:
http://kanze.james.neuf.fr/code-en.html, in the Basic
That's good advice, but the advice about choosing exceptions
or not based on how "exceptional" the situation is IMO ungood
advice -- no matter that it is advice that's (mindlessly,
and with reference to some "authority") repeated by a majority
of C++ programmers.
In addition to being so vague and subjective as to be
worthless, it's worthless because what can be "exceptional" in
one context need not be exceptional in some other context.
It's vague and subjective precisely because it depends on the
context. No one is claiming anything else. It's a sort of way
of saying that it depends, with a very vague suggestion about
what might be relevant to consider.
Instead, use exceptions where they provide clarity and are
most practical -- same advice as for use of loop constructs
and other language constructs.
OK. I'm not sure what "most practical" really means in this
context, but clarity is certainly important. Of course, which
solution is clearest will also depend on the application:
"provides clarity and are most pratical" is also vague and
Which is fine, because the issue is vague and subjective; since
the correct answer depends a lot on context, any context free
answer must be vague and subjective.
James Kanze (GABI Software) email:firstname.lastname@example.org
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34