AnyDAC
ContentsIndexHome
PreviousUpNext
TADQuery.OnError Event

The event fires when an error happens, while dataset is communicating with DBMS.

Group
Links
Parameters 
Description 
ASender 
The reference to dataset. 
AInitiator 
The reference to interface of object, which initially raised the exception. 
AException 
The exception object, which may be modified or even replaced by another exception object instance. 

The OnError event fires, when dataset executes one of the following operations and an error happens:

  • Prepare. The dataset is preparing DBMS command text for execution.
  • Open. The dataset is executing DBMS command to open cursor.
  • Execute, ExecSQL, ExecProc. The dataset is executing DBMS command text.
  • Navigation methods. The dataset is fetching rows from cursor.

If most cases AException object will of EADDBEngineException class. If AException is of EADDBArrayExecuteError class, then it is Array DML error handling case. You should use a code like in the first example. 

To centralize error handling, you can consider to use TADCustomConnection.OnError.

property OnError: TADErrorEvent;

Handling Array DML errors:

procedure TForm1.ADQuery1Error(ASender: TObject; const AInitiator: IADStanObject; var AException: Exception);
begin
  if AException is EADPhysArrayExecuteError then
    with EADPhysArrayExecuteError(AException) do
      if Errors[0].Kind = ekUKViolated then
        AAction := eaSkip
      else
        AAction := eaFail
  else
    ...
end;

Error substitution:

procedure TForm1.ADQuery1Error(ASender: TObject; const AInitiator: IADStanObject; var AException: Exception);
begin
  if EADDBEngineException(AException).Errors[0].Kind = ekPKViolated then begin
    AException.Free;
    AException := Exception.Create('Please, enter unique ID value');
  end;
end;
What do you think about this topic? Send feedback!