Name

ExceptProc Variable

Syntax

var ExceptProc: Pointer;

procedure MyProc(ExceptObject: TObject; ExceptAddr: Pointer);
begin ... end;
...
ExceptProc := @MyProc;

Description

If an exception is not handled in a try-except block and ExceptProc is not nil, Delphi calls the procedure that ExceptProc points to, passing as arguments a reference to the exception object and the address where the exception originated. If ExceptProc is nil, Delphi raises runtime error 217.

Example

// Log exceptions in an application event log (in Windows NT).
procedure LogException(ExceptObject: TObject; ExceptAddr: Pointer);
var
  ApplicationName: string;
  Handle: THandle;
  Msg: string;
  Strings: array[0..0] of PChar;
begin
  ApplicationName := ChangeFileExt(ExtractFileName(ParamStr(0)), '');
  // Change the application path to its base name, e.g., 'App'.
  // The application must have created the appropriate register key.
  Handle := RegisterEventSource(nil, PChar(ApplicationName));

  // Get the exception message.
  if ExceptObject is Exception then
    Msg := Exception(ExceptObject).Message
  else
    Msg := ExceptObject.ClassName;
  Strings[0] := PChar(Msg);

  // Define the Category and Exception_ID elsewhere.
  ReportEvent(Handle, EventLog_Error_Type, Category, Exception_ID,
    nil, 0, 1, @Strings, nil);

  DeregisterEventSource(Handle);
end;
...
ExceptProc := @LogException;

See Also

ErrorProc Variable, Raise Keyword

Get Delphi in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.