编写自己的异常处理句柄} procedure TMyClass.MyExceptionHandler(Sender: TObject; EInstance: Exception); var ErrorFile:TextFile; FileName,Content:string; FindFlag:Boolean; Begin {截获出现的新的异常,并存入文件ErrorInfo.txt.} FileName:=ApplicationPath+'ErrorInfo.txt'; { ApplicationPath 是在主Form中定义的全局变量,记 录应用程序所在的目录} {打开文件} AssignFile(ErrorFile,FileName); if not FileExists(FileName) then ReWrite(ErrorFile) else ReSet(ErrorFile); {检查出现的异常是否曾经记录在文件ErrorInfo.txt中} FindFlag:=False; While not SeekEof(ErrorFile) do begin Readln(ErrorFile,Content); if Pos(EInstance.ClassName+':'+EInstance.Message,Content)<>0 then begin FindFlag:=True; break; end; end; {如果是一个未被记录过的异常,则将它记录在文件中} if not FindFlag then begin ShowMessage('出现新的错误!'); Append(ErrorFile); Writeln(ErrorFile,EInstance.ClassName+':'+EInstance.Message); end;
关闭文件} CloseFile(ErrorFile); {对出现的异常显示中文提示} If EInstance is EDivByZero then Application.MessageBox('除数不能为零!','错误',MB_OK+MB_ICONSTOP) else if EInstance is EAccessViolation then Application.MessageBox('访问了无效的内存区域!','错误',MB_OK+MB_ICONSTOP) else if (EInstance is EDatabaseError) then Application.MessageBox('数据库操作出现错误!','错误',MB_OK+MB_ICONSTOP) else if (EInstance is EFOpenError) then Application.MessageBox('文件不能打开!','错误',MB_OK+MB_ICONSTOP) else if (EInstance is EConvertError) then Application.MessageBox('非法的类型转换!','错误',MB_OK+MB_ICONSTOP) else MessageDlg(EInstance.ClassName+':'+EInstance.Message,mtInformation,[mbOK],0) end;
procedure TMyClass.MyExceptionHandler(Sender: TObject; EInstance: Exception);
var
ErrorFile:TextFile;
FileName,Content:string;
FindFlag:Boolean;
Begin
{截获出现的新的异常,并存入文件ErrorInfo.txt.}
FileName:=ApplicationPath+'ErrorInfo.txt'; { ApplicationPath 是在主Form中定义的全局变量,记
录应用程序所在的目录}
{打开文件}
AssignFile(ErrorFile,FileName);
if not FileExists(FileName) then
ReWrite(ErrorFile)
else
ReSet(ErrorFile);
{检查出现的异常是否曾经记录在文件ErrorInfo.txt中}
FindFlag:=False; While not SeekEof(ErrorFile) do
begin
Readln(ErrorFile,Content);
if Pos(EInstance.ClassName+':'+EInstance.Message,Content)<>0 then
begin
FindFlag:=True;
break;
end;
end; {如果是一个未被记录过的异常,则将它记录在文件中}
if not FindFlag then
begin
ShowMessage('出现新的错误!');
Append(ErrorFile);
Writeln(ErrorFile,EInstance.ClassName+':'+EInstance.Message);
end;
CloseFile(ErrorFile);
{对出现的异常显示中文提示}
If EInstance is EDivByZero then
Application.MessageBox('除数不能为零!','错误',MB_OK+MB_ICONSTOP)
else if EInstance is EAccessViolation then
Application.MessageBox('访问了无效的内存区域!','错误',MB_OK+MB_ICONSTOP)
else if (EInstance is EDatabaseError) then
Application.MessageBox('数据库操作出现错误!','错误',MB_OK+MB_ICONSTOP)
else if (EInstance is EFOpenError) then
Application.MessageBox('文件不能打开!','错误',MB_OK+MB_ICONSTOP)
else if (EInstance is EConvertError) then
Application.MessageBox('非法的类型转换!','错误',MB_OK+MB_ICONSTOP)
else
MessageDlg(EInstance.ClassName+':'+EInstance.Message,mtInformation,[mbOK],0)
end;