【发布时间】:2013-05-25 23:52:30
【问题描述】:
我需要在 Delphi 7 程序中捕获所有非单元初始化异常,以便将异常写入文件,并可能向用户显示消息。
读完后,我认为全局异常处理程序会很麻烦,我只需要在 DPR 级别捕获所有异常。但是,我无法获得下面的代码来访问 dpr 中的 ShowMessage。
为什么下面的引发异常实际上会导致屏幕上显示异常,而不是跳出到 .dpr 的 except 子句?也许全局异常处理程序会更好?
dpr 中紧接在下面的代码不应该捕获表单中的所有异常吗?
在 DPR:
begin
Application.Initialize;
try
Application.CreateForm(TForm1, Form1);
Application.Run;
except
On E: Exception do
ShowMessage('In dpr except. Exception is: ' + E.Message);
end;
end.
形式:
Function TForm1.DoSomething( out aErrm: String):boolean; // force a failure for testing
begin
Result := FALSE;
aErrm := 'Failed in DoSomething';
end;
procedure TForm1.FormShow(Sender: TObject);
begin
try
fOk := DoSomething(fErrm);
except
fOk := FALSE;
Errm := 'Unexpected exception'
end;
if (NOT fOk) then
Raise Exception.Create(Errm) // why does this pop-up an exception when the DPR has an except around this code?
else
PostMessage(Handle, WM_CLOSE, 0, 0); // self-closing form
end; { FormActivate }
【问题讨论】:
-
我读了你两遍Q,不明白为什么你对
TApplication类提供的异常处理不满意,并有制作自己的冲动。 -
你不应该接受我的回答,@PeterVonča 切中要害。
标签: delphi