Delphi-Quellcode:
obj := TObject.Create;
try
try
// Some code
except
// Exception handling
end;
finally
obj.Free;
end;
Alternativ:
Delphi-Quellcode:
obj :=
nil;
try
obj := TObject.Create;
try
// Some code
except
// Exception handling
end;
finally
obj.Free;
end;
Die Alternativversion ist vor allem sinnvoll, wenns mehrere Objekte sind und man nicht ein try..finally pro Objekt habe will:
Delphi-Quellcode:
obj1 :=
nil;
obj2 :=
nil;
obj3 :=
nil;
try
obj1 := TObject.Create;
obj2 := TObject.Create;
obj3 := TObject.Create;
try
// Some code
except
// Exception handling
end;
finally
obj1.Free;
obj2.Free;
obj3.Free;
end;
statt
Delphi-Quellcode:
obj1 := TObject.Create;
try
obj2 := TObject.Create;
try
obj3 := TObject.Create;
try
try
// Some code
except
// Exception handling
end;
finally
obj3.Free;
end;
finally
obj2.Free;
end;
finally
obj1.Free;
end;