Boost logo

Boost :

From: David Abrahams (david.abrahams_at_[hidden])
Date: 2001-08-05 19:34:19


----- Original Message -----
From: "John Max Skaller" <skaller_at_[hidden]>

> Perhaps Dave can comment: I'm really quite interested
> in how to use EH well (um, I'd say 'Why EH doesn't
> really work' but I'm trying to be more politically acceptable :-)

Thanks for that ;-) I think you've mostly got the right idea. Obviously, EH
doesn't work well as a replacement for break. I think it's a mistake to try
to use it that way, no matter how much it looks like it's capable of making
up for break's many deficiencies.

> My initial experience with EH in C++ was that
> it greatly simplified code. More recently, however,
> I've been avoiding exceptions for anything other than
> catastrophic errors

What do you mean by "catastrophic"?

> and that in a language in which the
> implementation is efficient -- because the loss of detail
> control just isn't worth it.
>
> To make this latter point more precise by example:
> in a piece of code like:
>
> try {
> do_something();
> do_domething_else();
> }
> catch ...
>
> you are only handling an error in _either_ of the two
> calls.

Yes. For a large class of operations (among them many strong-guarantee
operations), the recovery/cleanup actions needed are independent of the
source of the error or the reason for the error. For those cases, EH makes
things much simpler.

> So if you write
>
> try {
> lookup();
> handle_result();
> }
> catch (not_found_error e)
> {
> handle_lookup_failure();
> }
>
> you might be in for a surprise if 'handle_result'
> itself contains a lookup which isn't trapped.
>
> What you _really_ wanted was:
>
> if (lookup()) handle_result()
> else handle_lookup_failure()
>
> and that's the best way to code it, IMHO.

I might be inclined to agree (depending on the details of the situation).
Conditions where an error can be locally recovered from such that the
enclosing operation may continue are rare in the programs I write. Where
they do occur, I have tended not to use EH to deal with it.

> EH, especially in C++, really is best for handling errors,

Mostly, with a few exceptions ;-)

-Dave


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk