Re: Exception Misconceptions: Exceptions are better than return
values
On Dec 17, 3:03 pm, "dragan" <spambus...@prodigy.net> wrote:
You hear this all the time: "Exceptions are better than return
values".
Where?
There's no silver bullet. For some types of errors, exceptions are
preferable. For others, return types are to be preferred. And there
are those where the only safe thing to do is to abort.
Or the variations on that theme. This, of course, is a misconception
when stated as a generality (without context). A major problem
attributed to return values, no guarantee that the programmer will
check the return value, is a language design or implementation issue:
the compiler should warn that a return value is not being
checked/assigned/whatever.
That would be logical, but for historical reasons, there are a lot of
functions in C (and thus callable from C++) which have return values
which interest no one. When was the last time you used the return
value
of strcpy, for example.
With compiler return value checking enforcement, analyses of when to
use an exception mechanism vs. a return code become much more
practical/palatable.
It's possible to design return code types such that they cause an
assertion failure if they're not checked. I've used them in some
applications. In this respect, there's probably even a slight
advantage
for return codes: to verify that an exception is correctly caught and
handled, you have to trigger it somehow---if the return code isn't
checked, the program will crash (assertion failure) even if there
wasn't
an error.
--
James Kanze