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

前端 未结 8 1305
攒了一身酷
攒了一身酷 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:25

    If you want to re-raise the exception only under certain conditions, write

    procedure TForm3.Button1Click(Sender: TObject);
    begin
      try
        raise Exception.Create('Bum');
      except
        on E: Exception do
        begin
          if MyHandleException(E) then
            raise;
        end;
      end;
    end;
    
    function TForm3.MyHandleException(AException: Exception): boolean;
    begin
      ShowMessage(AException.Message);
      result := true/false;
    end;
    

提交回复
热议问题