Top  Previous  Next

What is translated > Statements > finally


The finally keyword after a try block opens a block of code, which is executed regardless of what happened in the try block. Here some cleanup can be done and acquired resources can be freed. C++Builder has an according key word  __finally , which does the same in C++, but this is not a standard keyword. For other compilers finally statements have to be simulated. Delphi2Cpp takes a solution which is presented by Craig Scott::




By use of the presented OnLeavingScope class the translation of a try-finally statement looks as follows:





  obj : TObject;




  obj := TObject.Create(NIL);








#include "OnLeavingScope.h"


TObject* Obj = NULL;


  auto olsLambda = onLeavingScope([&] 


    delete Obj;


  Obj = new TObject(NULL);




olsLambda is a class, which gets a lambda function as parameter to its constructor. This function is stored internally and gets executed in the destructor of the class. The include "OnLeavingScope.h" is inserted automatically.













This page belongs to the Delphi2Cpp Documentation

Delphi2Cpp home  Content