1.安装jcl
2.安装jvcl
详细工作
窗口上放一个TApplicationEvents控件,并绑定OnException方法
代码如下
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, JclDebug, AppEvnts; type TForm1 = class(TForm) Button1: TButton; ApplicationEvents: TApplicationEvents; mmLogs: TMemo; procedure Button1Click(Sender: TObject); procedure ApplicationEventsException(Sender: TObject; E: Exception); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} //这是 TApplicationEvents.onException事件 procedure TForm1.ApplicationEventsException(Sender: TObject; E: Exception); begin mmLogs.Lines.Add(DateTimeToStr(Now) + ' ' + e.ClassName); mmLogs.Lines.Add('错误原因: ' + e.Message); JclLastExceptStackListToStrings(mmLogs.Lines, False, True, True, False); mmLogs.Lines.Add(''); Application.ShowException(E); end; procedure TForm1.Button1Click(Sender: TObject); var x,y : Integer; z : double; begin x := 10; y := 0; z := x /y; ShowMessage(floatToStr(z)); end; initialization Include(JclStackTrackingOptions, stRawMode); Include(JclStackTrackingOptions, stStaticModuleList); JclStartExceptionTracking; finalization JclStopExceptionTracking; end.
当按下button1的时候会发生一个除零异常,然后显示异常堆栈信息如下
2008-6-30 23:37:48 EZeroDivide
错误原因: Floating point division by zero
[0046E378] Unit1.TForm1.Button1Click (Line 45, "Unit1.pas" + 3) + $C
[0045206A] Forms.GetRealParentForm (Line 1897, "Forms.pas" + 3) + $8
[0043FE08] Controls.TControl.Click (Line 5229, "Controls.pas" + 9) + $8
[0042C862] StdCtrls.TButton.Click (Line 3745, "StdCtrls.pas" + 3) + $2
[0042C960] StdCtrls.TButton.CNCommand (Line 3797, "StdCtrls.pas" + 1) + $B
[0043F903] Controls.TControl.WndProc (Line 5146, "Controls.pas" + 83) + $6
[004438FB] Controls.TWinControl.WndProc (Line 7304, "Controls.pas" + 111) + $6
[0042C70C] StdCtrls.TButtonControl.WndProc (Line 3684, "StdCtrls.pas" + 13) + $4
[0043F590] Controls.TControl.Perform (Line 5021, "Controls.pas" + 5) + $C
[00443A4B] Controls.DoControlMsg (Line 7353, "Controls.pas" + 6) + $11
[00444443] Controls.TWinControl.WMCommand (Line 7616, "Controls.pas" + 1) + $5
[00457D10] Forms.TCustomForm.WMCommand (Line 5016, "Forms.pas" + 3) + $4
[0043F903] Controls.TControl.WndProc (Line 5146, "Controls.pas" + 83) + $6
[004438FB] Controls.TWinControl.WndProc (Line 7304, "Controls.pas" + 111) + $6
[00414F68] Classes.TThreadList.UnlockList + $4
[00424680] Graphics.FreeMemoryContexts (Line 5060, "Graphics.pas" + 12) + $5
[00454DFF] Forms.TCustomForm.WndProc (Line 3512, "Forms.pas" + 136) + $5
[00443024] Controls.TWinControl.MainWndProc (Line 7073, "Controls.pas" + 3) + $6
[0041BAE8] Classes.StdWndProc + $14
[004439F7] Controls.TWinControl.DefaultHandler (Line 7334, "Controls.pas" + 23) + $17
[00440228] Controls.TControl.WMLButtonUp (Line 5360, "Controls.pas" + 1) + $6
[0043F903] Controls.TControl.WndProc (Line 5146, "Controls.pas" + 83) + $6
[0041BAE8] Classes.StdWndProc + $14
[0044358D] Controls.TWinControl.WndProc (Line 7217, "Controls.pas" + 24) + $6
[00414F68] Classes.TThreadList.UnlockList + $4
[00424680] Graphics.FreeMemoryContexts (Line 5060, "Graphics.pas" + 12) + $5
[00443024] Controls.TWinControl.MainWndProc (Line 7073, "Controls.pas" + 3) + $6
[0044335F] Controls.TWinControl.IsControlMouseMsg (Line 7168, "Controls.pas" + 1) + $9
[004438FB] Controls.TWinControl.WndProc (Line 7304, "Controls.pas" + 111) + $6
[0042C70C] StdCtrls.TButtonControl.WndProc (Line 3684, "StdCtrls.pas" + 13) + $4
[00443024] Controls.TWinControl.MainWndProc (Line 7073, "Controls.pas" + 3) + $6
[0041BAE8] Classes.StdWndProc + $14
[0041C112] Contnrs.TComponentList.GetItems + $A
[0045CB7C] Forms.TApplication.ProcessMessage (Line 8105, "Forms.pas" + 23) + $1
[0045CB9E] Forms.TApplication.HandleMessage (Line 8124, "Forms.pas" + 1) + $4
[0045CE93] Forms.TApplication.Run (Line 8223, "Forms.pas" + 20) + $3
[0046FACD] Project1.Project1 (Line 13, "" + 4) + $7
其中的黑体部分就是出错的代码行了。