Google Groups Home
Help | Sign in
Message from discussion consensus on exceptions?
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
James Kanze  
View profile
 More options Dec 12 2000, 7:47 am
Newsgroups: comp.lang.c++.moderated
From: James Kanze <James.Ka...@dresdner-bank.com>
Date: 12 Dec 2000 07:47:51 -0500
Local: Tues, Dec 12 2000 7:47 am
Subject: Re: consensus on exceptions?

Gabriel Zachmann wrote:
> I am trying to decide whether or not to use exceptions.
> I searched this NG with deja
> and read some GotW (http://www.peerdirect.com/resources/)
> and some C++ tips (http://cpptips.hyperformix.com/Exceptions.html);
> in particular http://cpptips.hyperformix.com/cpptips/except_consensus
> was very informative.
> But I'm afraid I still don't see if there is a consensus
> whether or not
>                 Exceptions are a Good Thing?

There is no concensus.  Some people swear by them.  Others swear at
them.

> I found a lot of tips and discussion about the technicalities of
> exceptions, but the big question is
>  From what I read, there seems to be a consensus that:
> 1. Writing exception-safe code is harder than without exceptions;
> 2. Excpetions introduce a second path of control flow "behind the scenes",
>    which increases the complexity of the code;
> 3. In order to do decent error-handling in constructors, you must use
>    exceptions.

Add overloaded operators to 3, and you've summarized it pretty well.
On the other hand, it's possible to design constructors so they don't
fail, or so that you can check them afterwards (a la iostream), so
it's not necessarily as big a point as is sometimes pretended.

And you missed:

4. Exceptions make the main body of the function simpler, so easier to
   write and to understand.

I don't think there is one true answer.  Generally, I'm fairly
sceptical of exceptions, but then, I'm sceptical of unproven solutions
in general.  *IF* you have a lot of cases where error detection is at
a much lower level than error handling, exceptions can simplify the
intermediate code significantly.  On the other hand, they do create a
whole new family of problems involving exception safety, and the make
rigorous analysis of code flow more difficult.  Which aspect weighs
heaviest will vary according to the application and the organization.

Globally, I'd say that exceptions should not be used in a critical
application, where human life is at stake.  And of course, in a lot of
simple applications (compilers, etc.), they're really irrelevant -- in
exceptional cases, you just abort with an error message.  For typical
server applications, etc., however, they might have a place to abort
an operation without killing the process.

> So, my questions are:
> a) should I use exceptions only for constructors and error codes for
>    all other methods?

No.  Be consistent.

> b) if exceptions are used in general, should functions return *only*
>    error codes *or* throw exceptions? (i.e., is it ok to mix both in one
>    method?)

Ditto.

> c) should exceptions be used only for "exceptional" cases, or could they
>    also be used for not-so-exceptional things, like bogus parameters passed
>    in from the caller?

I would hope that a bogus parameter is something really exceptional,
if it comes from within my program.  I wouldn't use exceptions for
errors in user input, however.

--
James Kanze                               mailto:ka...@gabi-soft.de
Conseils en informatique orientée objet/
                   Beratung in objektorientierter Datenverarbeitung
Ziegelhüttenweg 17a, 60598 Frankfurt, Germany Tel. +49(069)63198627

      [ Send an empty e-mail to c++-h...@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google