How should I re-raise a Delphi exception after logging it?

前端 未结 8 1284
攒了一身酷
攒了一身酷 2021-01-01 10:40

Do you know a way to trap, log, and re-raise exception in Delphi code? A simple example:

procedure TForm3.Button1Click(Sender: TObject);
begin
  try
    rais         


        
8条回答
  •  鱼传尺愫
    2021-01-01 11:27

    Following on from Craig Young's post, I've used something along the lines of the following code successfully. You can preserve the original exception location by using the "at" identifier with the ExceptAddr function. The original exception class type and information is also preserved.

    procedure MyHandleException(AMethod: string);
    var
      e: Exception;
    begin
      e := Exception(AcquireExceptionObject);
      e.Message := e.Message + ' raised in ' + AMethod; 
      raise e at ExceptAddr;
    end;
    
    try
      ...
    except
      MyHandleException('MyMethod');
    end;
    

提交回复
热议问题