When high performant exceptions are at your disposal (as they are in C++) they should be used to indicate any error when possible.
O_o
Using exceptions is always possible; you can transform any error from any system into an exception.
It's usually okay even when error is expected (not "exceptional"), because the stack unwinding and error response is likely to be multitudes quicker than what ever operation would have succeed anyway.
No. The speed, simplicity, and cleanliness of recovery is not an appropriate reason to reach for exceptions; those reasons belong to scoped destruction, which allows "RAII", which still apply with return codes.
Code:
void exchange(int * p, int * q, int s)
{
if(*p == *q)
{
*p = s;
}
else
{
*q = *p;
throw(something());
}
}
// ...
bool done(false);
while(!done)
{
try
{
exchange(p, q, s);
done = true;
}
catch(...)
{
}
}
Code:
bool exchange(int * p, int * q, int s)
{
if(*p == *q)
{
*p = s;
return(true);
}
*q = *p;
return(false);
}
// ...
while(!exchange(p, q, s))
{
}
This example is a remarkably bad fit for exceptions, and infinitely many examples are available where perfectly normal error situations are signaled and examined as normal and expected parts of employing a mechanism. Furthermore, the memory referenced by `p' and `q' within the return code example could still be managed resources, from a smart pointer perhaps, where the code is obviously cleaner, simpler, and more efficient, in development terms, thanks to the nature of scoped destruction which is entirely unrelated to exceptions.
It is amazing how much cleaner code that makes heavy use of exceptions is than "well-balanced" code that you advocate is.
I advocate clean code.
I also advocate good code.
[Edit]
I don't, however, advocate "well-balanced code" as you intend; if exceptions are the right way to go, I advocate exceptions. I'm just have enough experience to not advocate exceptions when they are definitely the wrong approach.
[/Edit]
Unlike you, I understand that coding is a skill that requires a lot of dedicated thought not just mindlessly throwing a tool at a problem.
Again, if you simply use exceptions instead of returning an error code everywhere you are guaranteed to use exceptions poorly.
Of course, I'm happy to simplify: if you simply use any tool everywhere you are guaranteed to use every tool poorly.
Soma