Board index » delphi » constructors and destructors

constructors and destructors

Lets say I have the following:

TMyObject = class
    ListA,
    ListB: TList;
  public
    constructor Create; virtual;
    destructor Destroy; override;
  end;

implementation

constructor TMyObject.Create;
begin
  inherited Create;
  ListA := TList.Create;
  try       <----------------Should I check for exceptions here?
    ListB := TList.Create;
  except
    ListA.Free;
    raise;
  end;
end;

destructor TMyObject.Destroy;
begin
  try      <------------------Should I check here?
    ListA.Free;
  finally
    try
      ListB.Free;
    finally
      inherited Destroy;
    end;
  end;
end;

Any comments on how to deal with possible exceptions during object
construction and destruction?

 

Re:constructors and destructors


On Wed, 11 Sep 1996 17:56:28 -0400, Roger Hernandez

Quote
<rog...@shadow.net> wrote:
>Any comments on how to deal with possible exceptions during object
>construction and destruction?

If a constructor raises an exception, Delphi automatically calls
Destroy to destroy the partially constructed object.  See the Object
Pascal Language Guide for more information.
--
Ray Lischner, Author of Secrets of Delphi 2 (Waite Group Press)
Tempest Software, Corvallis, Oregon, USA  http://www.tempest-sw.com

Other Threads