|
Boost : |
From: Giovanni Piero Deretta (gpderetta_at_[hidden])
Date: 2007-09-18 04:32:27
On 9/13/07, Howard Hinnant <hinnant_at_[hidden]> wrote:
> I am requesting comments, both for and against a "sticky exception".
> [...]
> Do you see a use for such a class? Would you use it? Would you want
> libraries you're using to use it? Do you have real world example
> conditions which fit this use case? Or is it just evil (and why)?
>
I think it might be a little too much on the evil side. Wouldn't it
break otherwise exception safe (if admittedly not very elegant) code
like the following?
template<typename F>
bool foo(F f) {
bar = acquire-resource-bar()
bool failed = false;
try {
f();
} catch (....) {
failed = true;
}
release-resource-bar(bar);
return failed;
}
Foo would work correctly for any function-like objects, except for
those that throw
sticky exception.
void baz() {
sticky_exception s
throw s;
}
int main() {
foo(baz); // XXX leaks a bar!!
}
That is, you can't throw a sticky exception unless everything in the
call stack is capable of handling it, else you might leak. I know that
the preferred idiom for exception safety is RAII (Which is also
sticky-exception-safe), but sometimes it is cumbersome to write a RAII
wrapper just for one use.
HTH,
gpd
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk