[DUG] Related try..finally question

Dennis Chuah dennis_cs_chuah at hotmail.com
Fri Jul 21 14:45:38 NZST 2006


Depends on whether you are writing a client or server app.  On a client app:

List1 = Nil;
List2 = Nil;
try
  List1 = TList.Create;
  List2 = TList.Create;

finally
  List1.Free;
  List2.Free;
end;

Is sufficient.

On a server, you'd want to protect the Free with try ... except

finally
  try List1.Free; except end;
  try List2.Free; except end;
end;

This is so that if the first Free raises an exception, you don't end up with 
a memory leak.  The second try except is strictly not needed, but I always 
use it because at a later date, I may maintain the code and add more clean 
up code after the second Free and don't want to forget to put the try except 
in.

Obviously with two objects, you can use nested try finally, but if your code 
creates a dozen or so objects, it is much clearer to use the above code.

----- Original Message ----- 
From: "Conor Boyd" <Conor.Boyd at trimble.co.nz>
To: "NZ Borland Developers Group - Delphi List" <delphi at ns3.123.co.nz>
Sent: Friday, July 21, 2006 2:26 PM
Subject: [DUG] Related try..finally question


> I'm getting the impression here that a lot of people only have a 1:1
> relationship between try.finallys and procedures/functions?
>
> Sounds a bit dangerous.
>
> Should realistically be a try..finally for each object you instantiate
> in a method.
>
> Comments, anyone?
>
> C.
>
> _______________________________________________
> Delphi mailing list
> Delphi at ns3.123.co.nz
> http://ns3.123.co.nz/mailman/listinfo/delphi
> 


More information about the Delphi mailing list