我希望我的自定义错误消息显示而不是系统生成的错误消息.具体来说,在这种情况下,如果用户试图打开已经在使用的文件,我想捕获错误并显示我的消息,但系统总是打败我,并生成以下消息:"项目.. .raised异常类EFOpenError,消息'无法打开文件'文件路径和名称".进程无法访问该文件,因为它正被另一个进程使用'." 当我关闭此错误消息时,即显示我的消息.我想只显示我的信息.我不希望显示系统消息.有没有办法做到这一点?我的代码不起作用.
这是我的代码:
begin
//check if input file can be opened
try
if OpenDialog1.Execute then
begin
Application.ProcessMessages;
Memo1.Clear;
try
Memo1.Lines.LoadFromFile(OpenDialog1.FileName);
InputFile := TStringList.Create;
InputFile.LoadFromFile(OpenDialog1.FileName);
ActiveFileName := ExtractFileName(OpenDialog1.FileName);
lblActiveFileName.Caption := 'Active File: ' + ActiveFileName;
mmuDisplayClear.Enabled := True;
mmuAnalysisOptions.Enabled := True;
except on
EFOpenError do //if file does not exist or is in use
MessageDlgPos('File either does not exist or may be ' +
'in use!', mtError, [mbOK], 0, 300, 300);
end;
end;
finally
mmuDispInstructions.Enabled := True;
mmuDispData.Enabled := False;
end;
end;
Run Code Online (Sandbox Code Playgroud)
您看到调试器在应用程序执行之前捕获异常.这是完全正常的行为.只需按F9即可将异常传递给您的应用程序以进行正常处理.如果您不希望调试器显示异常,请将异常类类型放在调试器的Ignore列表中,或者将代码包装在断点中以禁用/启用调试器的异常处理.