|
Boost : |
From: David Abrahams (abrahams_at_[hidden])
Date: 2000-08-11 20:58:33
----- Original Message -----
From: "William Kempf" <sirwillard_at_[hidden]>
> No matter what school of thought you follow there may be a valid
> reason to throw an exception here. I'm still on the fence, but I'll
> illustrate why an exception may be appropriate:
>
> void foo::bar()
> {
> mutex::trylock lock(mx);
> // We forget to check if we locked and proceed to access
> // shared resources here!
> }
>
> Using an exception will make a lot of code harder to deal with,
> especially since failing to lock isn't really an exceptional
> condition here, but not using an exception could lead to disaster.
Unconvincing, IMO. We might just as easily have forgotten to use a lock at
all. If we forget to check whether there is a lock, we have a bug in our
code. I don't believe in using exceptions to report programming errors. In
this case, it is even likely to mask the bug.
> The only good thing is that it shouldn't be too likely that someone
> would fail to check the lock here, since they used a trylock instead
> of a simple lock.
Another criterion I failed to mention but probably should have:
In the case you are considering throwing an exception, is efficiency
important?
I think in this case it probably is. If you fail to acquire the lock, are
you really willing to pay the runtime cost of an exception, which is
typically much higher than that of the alternatives?
-Dave
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk