2005/5/9

     
 

finally

artefaktur

| References | Casting | Arrays | Import | instanceof | Package | Synchronization | Throwable | finally | Dangerous | Stack | String |

There is no 'finally' in ACDK. But you can use other constructs with the same effect.

In Java you can have following constructs:

// Java
void foo()
{
  try {
    allocateSomeRessources();
    doSomeThingWithRessources();
  } catch (Exception ex) {
    //oops.
  } finally {
    deallocateSomeRessources();
  }
}
This construct is not possible in acdk.

A normal use of finally is to ensure, that all ressources will be free, which may allocated in the try block. To get the same result, wrap the ressource in an controller class:

// C++ / ACDK
class AquireRessourceGuard
{
  Ressource& _ressource;
public:
  AquireRessourceGuard(Ressource& ress)
  : _ressource(ress)
  {
    _ressource.aquire();
  }
  ~AquireRessourceGuard()
  {
    _ressource.release();
  }
};

void foo()
{
  try {
    Ressource ress;
    AquireRessourceGuard _guard(ress);
    doSomeThingWithRessources(ress);
  } catch (RException ex) {
    // oops
  } // no finally
}
The trick is, that in case doSomeThingWithRessources() throws an exception all objects on stack will be deallocated, including calling their destructors.


 < prevConstructs(9 / 12) next >