Logging exceptions

From Lazarus wiki
Revision as of 19:24, 2 September 2010 by Chronos (talk | contribs)
Jump to navigationJump to search

Handling exceptions

Application.OnException

<delphi>procedure TMainForm.CustomExceptionHandler(Sender: TObject; E: Exception); const

 NewLine = #13#10;

var

 I: Integer;
 Frames: PPointer;
 Report: string;

begin

 Report := 'Program exception! ' + NewLine +
   'Stacktrace:' + NewLine + NewLine;
 if E <> nil then begin
   Report := Report + 'Exception class: ' + E.ClassName + NewLine +
   'Message: ' + E.Message + NewLine;
 end;
 Report := Report + BackTraceStrFunc(ExceptAddr);
 Frames := ExceptFrames;
 for I := 0 to ExceptFrameCount - 1 do
   Report := Report + NewLine + BackTraceStrFunc(Frames);
 ShowMessage(Report);
 Halt; // End of program execution

end;

procedure TMainForm.FormCreate(Sender: TObject); begin

 Application.OnException := @CustomExceptionHandler;

end;

procedure TMainForm.ButtonClick(Sender: TObject); begin

 raise Exception.Create('Test');

end;</delphi>


Getting stacktrace

Compiler parameters

  • -gl - generate line numbers for debug informations
  • -gm