delphi下使用jclDebug,在运行时显示详细的调试堆栈信息的范例

来源:互联网 发布:奇门排盘软件 编辑:程序博客网 时间:2024/06/08 14:33

delphi的异常信息比起java来太简单了,java的异常不但有错误提示,还有详细的堆栈信息甚至还能分级,可以一直追溯到最顶级的异常发生处,非常方便。相比较而言delphi的异常就太小儿科了。

不过jcl 提供的jclDebug多多少少的弥补了这个不足,通过jcldebug,也可以在运行时获取到详细的异常堆栈信息了。

这个功能简直太棒了,假如在24小时不间断运行的服务器上加上jclDebug,当错误发生时会自动记录出错的堆栈信息,开发人员可以分析错误发生时的状态和堆栈信息,根据堆栈信息中的错误代码行来修正错误。

这功能简直太棒了。

下面演示一下如何为程序加入jclDebug,使得程序可以获取详细堆栈信息

准备工作


1.安装jcl
2.安装jvcl


详细工作
窗口上放一个TApplicationEvents控件,并绑定OnException方法

代码如下
unit Unit1;interfaceuses  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


其中的黑体部分就是出错的代码行了。