Re: Exceptions vs. Error Codes
From: Alf P. Steinbach (alfps_at_start.no)
Date: 10/10/04
- Next message: Alf P. Steinbach: "Re: is this portable"
- Previous message: Phlip: "Re: is this portable"
- In reply to: Shane Groff: "Exceptions vs. Error Codes"
- Next in thread: John Harrison: "Re: Exceptions vs. Error Codes"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 10 Oct 2004 01:59:15 GMT
* Shane Groff:
> I know this is a recurring discussion (I've spent the last 3 days
> reading through threads on the topic), but I feel compelled to start
> it up again.
>
> After reading through the existing threads, I find myself convinced
> that exceptions are a better mechanism, for example because
> constructors and operators can't return errors
Constructors can produce error codes. A constructor can take a reference
or pointer argument, or it can store an error code somewhere else. One
main reason why that is not a _good idea_ is that the built-in mechanism
for cleaning up when an objected is allocated dynamically and the
constructor fails, requires an exception from the constructor, and that
similar techniques implemented by oneself have the same requirement; without
such automated cleanup there is a live but invalid object around.
Another main reason is that the approach is not general: additional arguments
cannot be tacked on to a copy constructor or, as you write, an operator, so
these would have to store their error codes in some accessible place.
The "live but invalid" argument is also the main argument for not using
two-phase construction (except when encapsulated by an object factory).
> and code that doesn't
> need to handle/translate/recover in the face of errors, but can just
> propagate the error is cleaner with exceptions.
Yup.
> However, those reasons are often not compelling enough to convince
> people who don't like exceptions (usually in my experience because
> they incorrectly think that exceptions are more 'dangerous' somehow
> than error codes, when in fact, they just don't see that it is just as
> dangerous to ignore an error code as it is to not handle an exception
> when it should be).
>
> Consider this idiom when using error codes:
>
> ErrorCode EcFoo()
> {
> ErrorCode ec;
> Check(EcBar());
> Check(EcBletch());
> ...
> Exit:
> return (ec);
> }
>
> Here, Check is a macro that checks the return code and jumps to Exit
> if an error occurs.
Macros are evil.
An alternative is to make the error code an object that requires a check()
or value() or succeeded() call or something in order not to throw an
exception.
Some folks have advocated that approach e.g. because it makes the code more
explicit; one main drawback is that all code everywhere must then check every
function call for possible error, giving C-style impenetrable code, and
another main drawback is that exceptions are needed for constructors and
such anyway, lest one wind up with "live but invalid" objects...
-- A: Because it messes up the order in which people normally read text. Q: Why is it such a bad thing? A: Top-posting. Q: What is the most annoying thing on usenet and in e-mail?
- Next message: Alf P. Steinbach: "Re: is this portable"
- Previous message: Phlip: "Re: is this portable"
- In reply to: Shane Groff: "Exceptions vs. Error Codes"
- Next in thread: John Harrison: "Re: Exceptions vs. Error Codes"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|