Newsgroups : Borland : borland.public.delphi.nativeapi.win32 : 2005 Apr : Trying ... Finally .... Failing
| Subject: | Trying ... Finally .... Failing |
| Posted by: | "Tom Peters" (tom_vip..@msn.com) |
| Date: | Wed, 27 Apr 2005 13:56:49 |
This is embarrassing. I believe that I've been using
try...finally/except...end wrong. Could you guys post the guidelines and
examples of when and how to use this basic Delphi structure.
I don't why it never clicked, maybe it became habit, to do:
var
aDemo: TICON;
begin
try
aDemo := TICON.Create;
.... More code
finally
FreeAndNil(aDemo);
end;
end;
Some may say yes/no that is acceptable. Because, if you're having
troubles creating such a simple object, then you have a much bigger problem.
BUT. If the object didn't get created the code underneath it may fail and
the stuff finally statement shouldn't get callled.
Something like the above, should be done like:
var
aDemo: TICON;
begin
try
aDemo := TICON.Create;
try
.... More code
finally
FreeAndNil(aDemo);
end;
except;
end;
end;
Correct?
I also know you could probably do a Assigned(aDemo) in the "finally" section
of the first example or something. I guess you could also:
//If you order stuff correctly
var
aDemo: TICON;
begin
try
aDemo := TICON.Create;
.... More code
FreeAndNil(aDemo);
except;
end;
end;
I can't believe how dumb I've been.