Difference between revisions of "Logging exceptions"
From Lazarus wiki
Jump to navigationJump to search (New page: =Handling exceptions= ==Application.OnException== <delphi>procedure TMainForm.CustomExceptionHandler(Sender: TObject; E: Exception); begin ShowMessage(E.Message); end; procedure TMai...) |
|||
Line 4: | Line 4: | ||
<delphi>procedure TMainForm.CustomExceptionHandler(Sender: TObject; E: Exception); | <delphi>procedure TMainForm.CustomExceptionHandler(Sender: TObject; E: Exception); | ||
+ | const | ||
+ | NewLine = #13#10; | ||
+ | var | ||
+ | I: Integer; | ||
+ | Frames: PPointer; | ||
+ | Report: string; | ||
begin | begin | ||
− | + | Report := 'Program exception! ' + NewLine + | |
− | end; | + | '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); | procedure TMainForm.FormCreate(Sender: TObject); |
Revision as of 20:24, 2 September 2010
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