如果您想要处理的唯一错误是具有"重复值"消息的错误,这将有效:
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;
Run Code Online (Sandbox Code Playgroud)
该EDatabaseError只是没有有关该错误的附加信息的通用excption类,以获取有关ADO一个错误的扩展信息,你必须使用TADOConnection.Errors属性时获得specifc错误代码Key violation引发异常,该检查Number和NativeError性能.
您可以在此处找到有关此主题的更多文档