如何用Application.OnException获取整个Application异常的问题??

Michaelyfj 2003-01-04 01:52:47
我想通过Application.OnException来得到应用程序的所有异常信息,但
由于在开发过程中,有些地方已经用try...except...end 进行了保护,
用Application.OnException是否可以得到这些异常信息??
...全文
818 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
lion_lh 2003-06-03
  • 打赏
  • 举报
回复
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure MyExceptionHandler(Sender : TObject; E : Exception );

end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.MyExceptionHandler(
Sender : TObject; E : Exception );
var
wRetVal : Word;
begin
wRetVal := MessageDlg(
{
E.Message contains the
actual error message
we'll customize it a bit...
}
'ERROR: ' + E.Message,

mtError,
mbAbortRetryIgnore,
0
);

case wRetVal of
mrAbort:
begin
{ handle "Abort" here... }
end;


mrRetry:
begin
{ handle "Retry" here... }
end;

mrIgnore:
begin
{ handle "Ignore" here... }
end;

else
begin
{
handle "other" action here...
for example, if user choose to
close the message box without
clicking on any buttons
}
end;
end;

{
you could also call the default
exception handler:

Application.ShowException( E );
}
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
Application.OnException :=MyExceptionHandler;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
try
strtoint('fdf');
except on E :Exception do
MyExceptionHandler(sender,E);
end;
end;

end.
Michaelyfj 2003-06-03
  • 打赏
  • 举报
回复
to yczyk(小周):
TF_errorhandle是如何定义的?
diruser 2003-01-08
  • 打赏
  • 举报
回复
up
Michaelyfj 2003-01-08
  • 打赏
  • 举报
回复
我要的是:使用了try......except......end后,在
Application.OnException的事件中也能捕捉到异常信息
Michaelyfj 2003-01-07
  • 打赏
  • 举报
回复
to yczyk(小周):
你这种方法对于用了try......except......end保护的异常,
也无法得到异常信息啊
shxhark 2003-01-06
  • 打赏
  • 举报
回复
以上已经说了,我便不说了
yczyk 2003-01-06
  • 打赏
  • 举报
回复
处理过程:
在主程序的Create事件中:
application.OnException:=HandleError;

procedure HandleError(Sender: TObject; E: Exception);
var
EForm:TF_errorhandle;
begin
Beep;
EForm:=TF_errorhandle.Create(self);
try
with EForm.re_error do
begin
with dm_shared.Q_info do
begin
close;
sql.clear;
sql.add('insert 事务日记表 (f_date,f_kind,f_message) values (:cdate,:ckind,:cmessage)');
parambyname('cdate').asdatetime:=now();
if E is EDBEngineError then
parambyname('ckind').asstring:='数据库错误'
else
parambyname('ckind').asstring:='系统错误';
parambyname('cmessage').asstring:=E.Message;
execsql;
end;
Lines.Add('错误信息及处理建议:');
Paragraph.Create(EForm.re_error);
Paragraph.LeftIndent:=4;
Paragraph.Numbering:=nsBullet;
if E is EDBEngineError then
Lines.Add('错误类型:数据库错误')
else
Lines.Add('错误类型:系统错误');
Lines.Add('错误原因:'+E.Message);
Paragraph.Numbering:=nsNone;
Lines.add('');
Lines.Add('处理建议:');
Paragraph.Create(EForm.re_error);
Paragraph.LeftIndent:=4;
Paragraph.Numbering:=nsBullet;
Lines.Add('1.重试一次刚才的操作');
Lines.Add('2.退出本系统再进入一次');
Lines.add('3.与开发商联系');
Paragraph.Numbering:=nsNone;
Lines.add('');
Lines.add('我们的联系方式:');
EForm.ShowModal;
end;
finally
EForm.free;
end;
b51 2003-01-06
  • 打赏
  • 举报
回复
可以
function HandleException(E:Exception):integer;
begin
ShowMessage('错误是:'+(e.Message));
end;
Procedure Tfrmmain.AppException(Sender:Tobject;E:Exception);
//主窗体
begin
HandleException(E);
end;


Application.OnException=AppException;
Michaelyfj 2003-01-06
  • 打赏
  • 举报
回复
高手帮忙啊
lihongyue 2003-01-06
  • 打赏
  • 举报
回复
to yczyk(小周):
我按照你的去做,可提示TF_errorhandle未被定义,TF_errorhandle是系统定义的吗?还需要引用什么窗体吗?谢谢你!
Michaelyfj 2003-01-06
  • 打赏
  • 举报
回复
对HandleException(E:Exception):integer函数要做如何处理?

5,392

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧