ErrorProc Variable
var ErrorProc: Pointer; procedureMyErrorProc
(ErrorCode: Integer; ErrorAddr: Pointer); begin ... end; ErrorProc := @MyErrorProc
;
When a runtime error occurs, and the
ErrorProc
variable is not nil
,
Delphi calls the procedure that ErrorProc
points
to. The procedure takes two arguments: the error number and the error
address.
If ErrorProc
is nil
, Delphi
calls RunError
to issue an error message and halt
the program. In a GUI application, Delphi shows the error message in
a dialog box. In a console application, Delphi prints the error
message to the console.
Delphi stores the error code for I/O errors separately. If
ErrorCode
is zero, most likely the error is an I/O
error. Call IOResult
to obtain the I/O error code.
// Log errors in an application event log (in Windows NT).
procedure LogError(ErrorCode: Integer; ErrorAddr: Pointer);
var
ApplicationName: string;
Handle: THandle;
Strings: array[0..0] of PChar;
begin
// Change the application path to its base name, e.g., 'App'.
// The application must have created the appropriate register key.
ApplicationName := ChangeFileExt(ExtractFileName(ParamStr(0)), ''),
Handle := RegisterEventSource(nil, PChar(ApplicationName));
if ErrorCode = 0 then
ErrorCode := IOResult;
// Get the exception message.
Strings[0] := PChar('Runtime error #' + IntToStr(ErrorCode));
// Define the Category elsewhere.
ReportEvent(Handle, EventLog_Error_Type, Category, ErrorCode,
nil, 0, 1, @Strings, nil);
DeregisterEventSource(Handle);
end;
...
ErrorProc := @LogError;