Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trap key violation

hi i'm trying to do some exception handling and intercept a repeated field value (key violation) error. From my searching for a solution ive seen many suggestions to trap all errors using

try
(enter code)
except on E: EDatabaseError do
showmessage (Error message);
end; 

but I'd like to respond specifically to a key violation, it uses an access table using ADO.

like image 332
Jeowkes Avatar asked Dec 05 '22 16:12

Jeowkes


2 Answers

This will work, if the only error you're wanting to handle is the one with the 'duplicate value' message:

try
  // Your code
except
  on E: EOleException do
  begin
    // The better way is to find out what E.ErrorCode is
    // for this specific exception, and handle it instead
    // of checking the string - you didn't provide the
    // ErrorCode, though.
    // If E.ErrorCode = <whatever> then
    //
    if Pos('duplicate value', E.Message) > 0 then
      // You've got a duplicate with the message above
      // Do whatever handles it
    else
      raise;
  end;
  // If you want to handle other exception types (eg., EDataBaseError),
  // you can do so here:
  //  on E: EDataBaseError do
  //    HandleDBError;
end;
like image 92
Ken White Avatar answered Dec 30 '22 13:12

Ken White


The EDatabaseError is just a generic excption class without additional information about the error, to get extended information about a error in ADO, you must use the TADOConnection.Errors property to get the specifc error code when a Key violation exception is raised, for this check the Number and NativeError properties.

You can found more documentation about this topic here

  • ADO Error Codes
  • Error Object
like image 21
RRUZ Avatar answered Dec 30 '22 11:12

RRUZ