Re: C++0x
Triple-DES ha scritto:
Another possible solution is:
struct finally{};
void f()
{
try
{
//...
try
{
//...
}
catch(const SomeException&)
{
//...
}
//...
throw finally();
}
catch(...)
{
// emulates finally
}
}
This is not a good solution, however, because it incurs the cost of
throwing an exception even in the "no-exception-thrown" case and that
any exception thrown by inner try-block is caught even if the intent
would be to let it pass. The correct code is:
void f()
{
try
{
... // block contents here (may include try-blocks or not)
// finally block here
}
catch(...)
{
// finally block here (again)
throw; // rethrow exception
}
}
as you can see, there is an unfortunate code duplication that makes this
approach not very appealing.
The newly introduced C++0x exception propagation facility may suggest a
different approach, which unfortunately is currently ill-formed and
*will remain* ill-formed. I report it here just to provide an insight
about how a finally block could be implemented in C++:
void f()
{
try
{
// block contents here
goto finally; // illegal!
}
catch(...)
{
finally:
// finally block here
if (exception_ptr e = current_exception())
{
rethrow_exception(e);
}
}
}
HTH,
Ganesh
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]