Topic: Exceptions are a Good Thing! (was: Exceptions: Are they a Bad Thing?)


Author: joe@bftsi0.UUCP (Joe Foster of Borg)
Date: 20 Jan 94 20:01:43 GMT
Raw View
In article <2hjmplE49t@uni-erlangen.de>, hmkirchh@cip.informatik.uni-erlangen.de (Hans-Joachim Kirchhoff) writes:
> Several times now I saw the "old" return errorcode method being preferred
> to the new throw catch mechanism. However I cannot see any differences and
> certainly no advantages in writing

[example deleted]

There is no advantage to returning "magic" and inconsistent error
codes. It's not too hard to remember that fopen() returns 0 on
error and getw() returns -1 on error. Multiply the number of
functions whose error reporting you have to memorize by an order
of magnitude or three, though... Also, when getw() returns -1, is
it because that's what it read, or is it because something bad
happened? Uh oh...

> The only thing I don't know is, wether catch() is a valid C++ construct. If
> it isn't, it should be. catch() would catch all exceptions just as default
> "catches" all switch values.

It's called catch(...).

> throw ; is a valid C++ expression (at least with the IBM C++/2) and will
> rethrow the last thrown object.

Nice, isn't it? I wish the guy who told his managers that fire
will rain down from the sky if they use exception handling in
their C++ projects because exception handling in Ada wasn't
perfect had actually looked at C++ exception handling. :-)

> It has been argued, that return codes are more "visible" and force the
> programmer to "think" about errors. But the above statements are so similar
> to each otch other except that the exception mechanism is more powerful,
> that I cannot see any advantage in using return codes instead nor any
> disadvantage using exceptions.

Magic error values does require you to think about errors.
Unfortunately, a coder's attention has only to wander for a
moment for disaster to strike. Exception handling is better,
even if only because an uncaught exception will cause a program
to terminate instead of become deranged at random times.

[.sig snipped]

--
Joe Foster (joe@bftsi0.UUCP)
WARNING: I cannot be held responsible for the above        They're   coming  to
because  my cats have  apparently  learned to type.        take me away, ha ha!