public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Jason Cipriani" <jason.cipriani@gmail.com>
To: "Ted Byers" <r.ted.byers@rogers.com>
Cc: gcc-help@gcc.gnu.org
Subject: Re: try, finally
Date: Fri, 21 Mar 2008 02:26:00 -0000	[thread overview]
Message-ID: <c09049bf0803201925y1347e3f7v357adf2c3dd3062f@mail.gmail.com> (raw)
In-Reply-To: <457283.62826.qm@web88308.mail.re4.yahoo.com>

Ted, I can't respond to everything right now, because I somehow
managed to spend a full hour typing that last email. Hopefully some of
what you say here, I have addressed there. There are two things in
particular that I want to point out, though:

The first:

On Thu, Mar 20, 2008 at 10:49 AM, Ted Byers <r.ted.byers@rogers.com> wrote:
>  Look, C++ has a whole suite of error detection and
>  handling capability.  To use only exceptions is
>  somewhat like a carpenter having only a hammer trying
>  to hammer screws into place.  If all you have is a
>  hammer, everything looks like a nail.  But if you have
>  a set of screw drivers in your belt along with your
>  hammer, you'll use the right tool for the job.

I believe I addressed this in my recent post. Using *only* exceptions
would be a mistake. However, using error codes when exceptions can
make things a lot simpler is also a mistake. The same goes both ways,
"to use only error codes" fits into that analogy as well. There is a
happy medium, and there are some situations where exceptions are more
appropriate, and some where error codes are more appropriate. I take
the approach of using exceptions unless I am forced to do something
else, for example, the following situations:

1) Where performance is critical and exceptions have too much
overhead. For example, if I was writing, say, a function that
intersected a ray with a cylinder to use in a ray tracer
implementation (or whatever), I would certainly not have that function
thrown an exception if the ray did not intersect.

2) Where exceptions simply can't be used, such as in the boundary
conditions mentioned above (especially across C boundaries). In this
case, error codes are the best tool, and eventually they can be used
to throw exceptions at a higher level if that is appropriate.

I do not support using only exceptions. I also do not support using
only error codes. In college, my friends used to call me Jason "Use
the Right Tool For the Job" Cipriani. Well, maybe not. My original
question about SEH was in a situation where SEH was a great tool for
what I was trying to do. The book I just wrote about exceptions is
intended only to show a good example of when exceptions are a better
tool than error codes.

The second thing is:

>  You're throwing everywhere.  It is almost as if you
>  have a bad case of cyber-stomache flu.

LOL. :-) It's better than cyber-diarrhea I suppose.

> You are using
>  exceptions for flow control.

I need to address this because it is not correct. I am using
exceptions for *error handling* flow control. All error handling
involves some sort of trivial flow control. However, do not confuse
this with using exceptions for things like this:

void DoSomething (vector<int> &data) {

  int i = 0;

  try {
    while (true) {
      DoSomethingElse(data[i]);
      ++ i;
      if (i >= data.size()) throw int;
    }
  } catch (...) { }

  // do more stuff

}

NO. DO NOT DO THAT! (heh). That is *not* what exceptions are for at
all. Exceptions are for error conditions that must result in special
handling. I hope that I haven't given an example otherwise -- if I
have I didn't intend for it to look that way.

> That is not what they're
>  there for.  Eljay's brilliant description of return
>  codes (including especially his smart return code
>  objects) is a much better option.  And don't forget
>  assertions have a valid role in many cases also.

Assertions are good for stating assumptions; they notify you when your
assumptions are invalid and also double as documentation when reading
code. However, doing something like this:

FILE *f = fopen("file", "r");
assert(f);

Is *not* a good use for assert(), for hopefully obvious reasons. I
used fopen() as an arbitrary example, substitute your favorite
function instead.


I have read the rest of your reply, but I can not respond to it right
now. I apologize. But I did read it. Thanks.

Jason

  reply	other threads:[~2008-03-21  2:26 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-03-19 16:13 Jason Cipriani
2008-03-19 16:14 ` Jason Cipriani
2008-03-19 17:02   ` Tim Prince
2008-03-19 16:37 ` Brian Dessent
2008-03-19 17:31 ` Ted Byers
2008-03-19 17:37 ` me22
2008-03-19 19:09   ` Jason Cipriani
2008-03-19 19:09     ` me22
2008-03-20  5:50       ` Jason Cipriani
2008-03-20 13:30       ` Noel Yap
2008-03-21  2:27         ` Jason Cipriani
2008-03-19 21:04     ` Ted Byers
2008-03-20  6:28       ` Jason Cipriani
2008-03-20 12:24         ` John Love-Jensen
2008-03-21  2:11           ` Jason Cipriani
2008-03-21  2:37             ` me22
2008-03-21  2:45               ` Jason Cipriani
2008-03-21 15:24             ` Noel Yap
2008-03-21 22:50             ` Brian Dessent
2008-03-21 23:14               ` Noel Yap
2008-03-20 14:50         ` Ted Byers
2008-03-21  2:26           ` Jason Cipriani [this message]
2008-03-25 22:28 ` Ian Lance Taylor

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=c09049bf0803201925y1347e3f7v357adf2c3dd3062f@mail.gmail.com \
    --to=jason.cipriani@gmail.com \
    --cc=gcc-help@gcc.gnu.org \
    --cc=r.ted.byers@rogers.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).