Einzelnen Beitrag anzeigen

Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
43.157 Beiträge
 
Delphi 12 Athens
 
#9

AW: AcquireExceptionObject() / ReleaseExceptionObject()

  Alt 31. Mai 2017, 16:12
Hatte ich doch gesagt
* ReleaseExceptionObject macht halt nichts, wie schon richtig gesehn, aber sooooo sehr stört es jetzt nicht
* aber AcquireExceptionObject funktioniert dennoch (wäre blöd, wenn nicht)

Hatte das auch grade mal schnell als Test gebastelt:
Delphi-Quellcode:
type
  EMyTest = class(Exception)
  public
    // TObject
    procedure AfterConstruction; override;
    procedure BeforeDestruction; override;
    class function NewInstance: TObject; override;
    procedure FreeInstance; override;
    //destructor Destroy; override;
  protected
    // Exception
    procedure RaisingException(P: PExceptionRecord); override;
  public
    // Exception
    constructor Create(const Msg: string);
    destructor Destroy; override;
  end;

procedure EMyTest.AfterConstruction;
begin
  //Form5.Memo1.Lines.Add('Exception.AfterConstruction');
  inherited;
end;

procedure EMyTest.BeforeDestruction;
begin
  //Form5.Memo1.Lines.Add('Exception.BeforeDestruction');
  inherited;
end;

constructor EMyTest.Create(const Msg: string);
begin
  Form5.Memo1.Lines.Add('Exception.Create ' + Msg);
  inherited;
end;

destructor EMyTest.Destroy;
begin
  Form5.Memo1.Lines.Add('Exception.Destroy ' + Message);
  inherited;
end;

procedure EMyTest.FreeInstance;
begin
  //Form5.Memo1.Lines.Add('Exception.FreeInstance');
  inherited;
end;

class function EMyTest.NewInstance: TObject;
begin
  //Form5.Memo1.Lines.Add('Exception.NewInstance');
  Result := inherited;
end;

procedure EMyTest.RaisingException(P: PExceptionRecord);
begin
  Form5.Memo1.Lines.Add('Exception.RaisingException');
  inherited;
end;

procedure TForm5.FormCreate(Sender: TObject);
var
  E: {Exception}TObject;
begin
  {$REGION 'normal'}
  Memo1.Lines.Add('');
  Memo1.Lines.Add('***** normal');
  try
    Memo1.Lines.Add('raise Exception.Create()');
    //raise Exception.Create('Fehlermeldung');
    raise EMyTest.Create('Fehlermeldung');
    Memo1.Lines.Add('');
  except
    Memo1.Lines.Add('except ' + Exception(ExceptObject).Message);
    Memo1.Lines.Add('end');
  end;
  Memo1.Lines.Add('finish');
  {$ENDREGION}

  {$REGION 'release'}
  Memo1.Lines.Add('');
  Memo1.Lines.Add('***** release');
  try
    Memo1.Lines.Add('raise Exception.Create()');
    //raise Exception.Create('Fehlermeldung');
    raise EMyTest.Create('Fehlermeldung');
    Memo1.Lines.Add('');
  except
    Memo1.Lines.Add('except ' + Exception(ExceptObject).Message);
    Memo1.Lines.Add('ReleaseExceptionObject');
    ReleaseExceptionObject;
    Memo1.Lines.Add('end');
  end;
  Memo1.Lines.Add('finish');
  {$ENDREGION}

  {$REGION 'acquire'}
  Memo1.Lines.Add('');
  Memo1.Lines.Add('***** acquire');
  try
    Memo1.Lines.Add('raise Exception.Create()');
    //raise Exception.Create('Fehlermeldung');
    raise EMyTest.Create('Fehlermeldung');
    Memo1.Lines.Add('');
  except
    Memo1.Lines.Add('except ' + Exception(ExceptObject).Message);
    Memo1.Lines.Add('AcquireExceptionObject');
    E := AcquireExceptionObject;
    Memo1.Lines.Add('end');
  end;
  Memo1.Lines.Add('E.Free');
  E.Free;
  Memo1.Lines.Add('finish');
  {$ENDREGION}

  {$REGION 're-raise'}
  Memo1.Lines.Add('');
  Memo1.Lines.Add('***** re-raise');
  try
    try
      Memo1.Lines.Add('raise Exception.Create()');
      //raise Exception.Create('Fehlermeldung');
      raise EMyTest.Create('Fehlermeldung');
      Memo1.Lines.Add('');
    except
      Memo1.Lines.Add('except ' + Exception(ExceptObject).Message);
      Memo1.Lines.Add('raise E');
      raise;
      Memo1.Lines.Add('end');
    end;
    Memo1.Lines.Add('finish');
  except
    Memo1.Lines.Add('except(2) ' + Exception(ExceptObject).Message);
    Memo1.Lines.Add('end(2)');
  end;
  Memo1.Lines.Add('finish(2)');
  {$ENDREGION}

  {$REGION 'new-raise'}
  Memo1.Lines.Add('');
  Memo1.Lines.Add('***** new-raise');
  try
    try
      Memo1.Lines.Add('raise Exception.Create(1)');
      //raise Exception.Create('Fehlermeldung_1');
      raise EMyTest.Create('Fehlermeldung_1');
      Memo1.Lines.Add('');
    except
      Memo1.Lines.Add('except ' + Exception(ExceptObject).Message);
      Memo1.Lines.Add('raise Exception.Create(2)');
      raise EMyTest.Create('Fehlermeldung_2');
      Memo1.Lines.Add('end');
    end;
    Memo1.Lines.Add('finish');
  except
    Memo1.Lines.Add('except(2) ' + Exception(ExceptObject).Message);
    Memo1.Lines.Add('end(2)');
  end;
  Memo1.Lines.Add('finish(2)');
  {$ENDREGION}


  {$REGION 'acquire + re-raise'}
  Memo1.Lines.Add('');
  Memo1.Lines.Add('***** acquire + re-raise');
  try
    Memo1.Lines.Add('raise Exception.Create()');
    //raise Exception.Create('Fehlermeldung');
    raise EMyTest.Create('Fehlermeldung');
    Memo1.Lines.Add('');
  except
    Memo1.Lines.Add('except ' + Exception(ExceptObject).Message);
    Memo1.Lines.Add('AcquireExceptionObject');
    E := AcquireExceptionObject;
    Memo1.Lines.Add('end');
  end;
  try
    raise E;
  except
    Memo1.Lines.Add('except ' + Exception(ExceptObject).Message);
    Memo1.Lines.Add('end');
  end;
  Memo1.Lines.Add('finish');
  {$ENDREGION}
end;
Code:
***** normal
raise Exception.Create()
Exception.Create Fehlermeldung
Exception.RaisingException
except Fehlermeldung
end
Exception.Destroy Fehlermeldung
finish

***** release
raise Exception.Create()
Exception.Create Fehlermeldung
Exception.RaisingException
except Fehlermeldung
ReleaseExceptionObject
end
Exception.Destroy Fehlermeldung
finish

***** acquire
raise Exception.Create()
Exception.Create Fehlermeldung
Exception.RaisingException
except Fehlermeldung
AcquireExceptionObject
end
E.Free
Exception.Destroy Fehlermeldung
finish

***** re-raise
raise Exception.Create()
Exception.Create Fehlermeldung
Exception.RaisingException
except Fehlermeldung
raise E
except(2) Fehlermeldung
end(2)
Exception.Destroy Fehlermeldung
finish(2)

***** new-raise
raise Exception.Create(1)
Exception.Create Fehlermeldung_1
Exception.RaisingException
except Fehlermeldung_1
raise Exception.Create(2)
Exception.Create Fehlermeldung_2
Exception.RaisingException
Exception.Destroy Fehlermeldung_1
except(2) Fehlermeldung_2
end(2)
Exception.Destroy Fehlermeldung_2
finish(2)

***** acquire + re-raise
raise Exception.Create()
Exception.Create Fehlermeldung
Exception.RaisingException
except Fehlermeldung
AcquireExceptionObject
end
Exception.RaisingException
except Fehlermeldung
end
Exception.Destroy Fehlermeldung
finish
Tja, das kaputte ReleaseExceptionObject müsste ja eigentlich so aussehn, wenn es funktionieren würde:
Code:
***** release
raise Exception.Create()
Exception.Create Fehlermeldung
Exception.RaisingException
except Fehlermeldung
ReleaseExceptionObject
Exception.Destroy Fehlermeldung <<<<<<<<<<<<<<<<<
end
finish
anstatt
Code:
...
ReleaseExceptionObject
end
Exception.Destroy Fehlermeldung <<<<<<<<<<<<<<<<<
finish
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests

Geändert von himitsu (31. Mai 2017 um 16:23 Uhr)
  Mit Zitat antworten Zitat