I know destructor shouldn’t not throw exception.
http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.13
I have the following code :
~a()
{
cleanup();
}
// I do not expect exception being thrown in this function.
// If exception really happen, I know that it is something not recoverable.
void a::cleaup()
{
delete p;
}
In my static source code analysis, it complains that I shall call the cleanup function in this way :
~a()
{
try {
cleanup();
}
catch(...) {
// What to do? Print out some logging message?
}
}
// I do not expect exception being thrown in this function.
// If exception really happen, I know that it is something not recoverable.
void a::cleaup()
{
delete p;
}
I am not sure whether this is a good practice, to place try…catch block in destructor, whenever it calls functions. As :
(1) If cleanup function able to throw exception, I know something bad had happened. I prefer it to be fail-fast. That is, just let the whole system crash, and let the programmer debugs it.
(2) Overhead occur while entering and exiting try…catch block.
(3) The code look cumbersome, with a lot of try…catch block around classes’ destructor.
I may miss out some other points, why try…catch block should be in place.
Thanks.
Since
deletewon’t throw, neither willcleanupand as such there is no need to put the call in try-catch.Since your static analysis tool is probably having a hard time figuring that out, perhaps you could help it out (it’s only a guess though) by declaring
cleanupas no-throw.