簡體   English   中英

Delphi保護繼承的Destrutor免受異常影響

[英]Delphi protect inherited Destrutor from exception

我有一個擴展TFileStream的TMyFileStream。

type
   TMyFileStream = class(TFileStream)

在TMyFileStream.Destroy中,我有一些用於記錄服務器下載的過程。 當客戶端與服務器的關閉連接或響應的終止下載時,文件流將在響應流中傳遞。對象將被銷毀,析構函數日志信息(如字節發送/文件大小)將被破壞。

此過程可能首先引發調用父析構函數的異常。 我想保護父析構函數免受子異常的侵害。

基本例子

destructor TMyFileStream.Destroy;
begin
   MyExceptionMethod();

   inherited;
end;

我發現的第一個解決方案是首先調用繼承的析構函數

destructor TMyFileStream.Destroy;
begin
   inherited;

   MyExceptionMethod();
end;

另一個解決方案是捕獲並忽略異常

destructor TMyFileStream.Destroy;
begin
   try
      MyExceptionMethod();
   except
      // nothing
   end;

   inherited;
end;

另一個解決方案是捕獲並最終調用繼承的析構函數

destructor TMyFileStream.Destroy;
begin
   try
      try
         MyExceptionMethod();
      except
         // nothing
      end;
   finally
      inherited;
   end;
end;

正確的方法是什么?

一種替代方法是重寫BeforeDestruction ,那里inherited的實例實例內存的實際取消分配無關。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM