Search code examples
delphiexceptionfiredac

FireDac and aborting a before post, with system.abort


In continue to this question,when using FireDac, and calling in BeforePost event to a function that calls, Abort, cause a full system abort that breaks the loop that running around that abort.

while not fdOtherQuery.eof do begin
  fdQuery.insert;
  fdquery.fields[0].asstring := fdOtherQuery.fields[0].asstring;
  fdquery.post;
fdOtherQuery.next;
end;

Before post:

procedure TForm1.AzureDayarKriaAdditionsBeforePost(DataSet: TDataSet);
begin
  calculcation;  
end;

procedure calculaction;
begin
  if fdQuery.fields[0].asstring = 0 then abort;
end;

In case the abort in calculation is called, then the

while not fdOtherQuery.eof do begin

is also stoped


Solution

  • Abort raises a silent exception, which can be caught with try ... except.

    while not fdOtherQuery.eof do begin
      fdQuery.insert;
      fdquery.fields[0].asstring := fdOtherQuery.fields[0].asstring;
      try
        fdquery.post;
      except
        on E: EAbort do
        begin
          // log the error (when needed)
        end;
      end;   
      fdOtherQuery.next;
    end;