tags:

views:

131

answers:

5

In my destructor, I have to clean up a few resources. Let say I have three calls to clear resources that could throw. Since it is not good for letting an exception to leave a destructor, what should be my design pattern? Apparently the way below is not scalable.

Thanks.

class B::~B(){

try{
   clearResourceA()
 }
 catch{
     try{
         clearResourceB();
        } 
     catch{
         clearResourceC();
     }
     clearResourceC();
 }
clearResourceB();
    .
    .
}
+2  A: 

Capture anything that can throw in your destructor with a catch-all (i.e., catch (...)) and do your best to handle the exceptions thrown. Make sure that no exceptions propagate out of the destructor, which the catch-all will help you prevent.

Michael Goldshteyn
+7  A: 

Why not:

try{clearResourceA();} catch(...){}
try{clearResourceB();} catch(...){}
try{clearResourceC();} catch(...){}
Benoit
A: 
try   
{  
   ClearResourceA();  
}  
catch(...)  
{  
   ExceptionWasThrown();  
}  
try   
{  
   ClearResourceB();   
}  
catch(...)  
{  
   ExceptionWasThrown();  
}  
...
Armen Tsirunyan
Offtop: How can I enforce indentation?
Armen Tsirunyan
@Armen Tsirunyan - Use the "code" button, not the back ticks. The code button has a 101010 on it. Type in your code (indented), highlight the code, then push the code button.
Starkey
@Starkey: Unfortunately today there are neither those buttons nor the preview. I asked on meta but still pending answer
Armen Tsirunyan
@Armen: four initial spaces implies "code", there's no need for backticks as well.
Steve Jessop
@Steve: Thanks :)
Armen Tsirunyan
+5  A: 

Encapsulate each resource in a class which clears them in its destructor (with a surrounding try/catch):

struct ProperlyManagedA {
    // some means of using the resource - a rudimentary way is this:
    A &getA() { return a; }
    const A &getA() const { return a; }
    // cleanup
    ~ProperlyManagedA() { 
        try {
            a.clear(); // whatever it is ClearResourceA actually does
        } catch (...) {}
    }
  private:
    A a;
}

A shared_ptr with a custom deleter is one way to achieve this without having to create a whole class for each type of resource.

You might improve on discarding the exception (log the problem, for example), depending what's thrown.

Even better, modify resources A, B and C so that they clear themselves in their own destructors. That might not be possible, though.

Either way, you can then put as many such resources in a single class as you like, without adding any code to the destructor of the class. This is "scalability". The whole point of RAII, is that each user of a resource shouldn't have to write cleanup code in order to use the resource correctly.

Steve Jessop
Well sort-of. The purpose of RAII is that the cleanup is all done in your destructors so you don't call delete or freeHandle or whatever functions inside the bodies of your code. The main issue here is that clean-up code generally should not throw in the first place.
CashCow
@CashCow: "cleanup is all done in your destructors" - well, if the resources are designed with RAII principles, then cleanup is done in the destructor of the resource. If I have to write the RAII class myself, then admittedly that is my destructor. But whoever writes the RAII resource, users of that RAII resource don't have to write or explicitly call cleanup code. That's what I meant by "users".
Steve Jessop
@Steve: thanks, I was going insane while looking at the other suggestions :/
Matthieu M.
This is the only idiomatic answer. If you write C++ and don't use RAII, you're doing it wrong.
Pedro d'Aquino
+2  A: 

You could also wrap your ClearResources functions to ensure they never throw. Why might they throw anyway?

CashCow
Some kind of flush (or other I/O) is the usual reason.
Steve Jessop