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.
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;
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
Error Object
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With