try…finally Wizard

The try..finally wizard inserts a try .. finally” statement
and inserts the code for the finally block based on preceding code.

The wizard can be applied on existing code in which case the selection (or line) is
moved into the try block, similar to surround with template.

Pascal Example: try..finally Free

In a similar Pascal Win32 example, you could deceide use a TStringList buffer, fill that instead of the listbox and then assign it at once the list box.Items.
The first thing to do would be to create the buffer stringlist which itself is a typical case for the [page:articles\add-local-var “add local var”] refactoring.


After pressing Ctrl+Alt+J to invoke the wizard:


As you see: again the wizard inserted try finally statement and this time detected that the Buffer should be free’d.

Of course you’d still need to change the code to fill the buffer rather than the listbox etc. but that’s another story.

Example 3 : try..finally Free on new code

This would of course also have worked in the following situation:


Were the wizard would insert this:


 

Customizing the wizard

The wizard is customizable using a look-up list in file TryFinallyWizard.txt in the shared directory,
for example “C:\Program Files\ModelMakerTools\Shared”.
Here you define word substitution pairs. Substitution pairs are checked in order of appearance if occuring on whole word bounderies.
If nothing is substituted, the code to free an object is inserted.