问题描述
问题摘要分配给 Application.OnException
的方法在未处理的异常发生时永远不会运行。
Problem summary: The method assigned to Application.OnException
never runs when an unhandled exception occurs.
我创建一个只有这个单元的空白项目,并在 Unit.dfm
上放置一个按钮(这是基于一个):
I create a blank project with only this unit and place a single button on Unit.dfm
(this is based on an official example) :
// Unit1.pas
// *********
type
TForm1 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure AppException(Sender: TObject; E: Exception);
procedure Button1Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
Application.OnException := AppException;
end;
procedure TForm1.AppException(Sender: TObject; E: Exception);
begin
Application.Terminate;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
raise Exception.Create('Incorrect password entered');
end;
然后我在中设置断点TForm1.AppException()
。我运行程序,点击按钮,出现一个错误对话框,显示输入错误的密码,但如果我继续执行,断点不会中断;程序不是终止
就像我也问过。程序继续运行,我可以再次按下按钮。
Then I set a breakpoint inside TForm1.AppException()
. I run the program, click the button, an error dialog is shown saying "Incorrect password entered" but if I continue execution the breakpoint never breaks; the program doesn't Terminate
like I asked it too. The program continues running and I can press the button again.
我在Delphi 7中尝试了相同的代码(改编),但遇到相同的结果。
I tried the same code (adapted) in Delphi 7 but the same result is encountered.
推荐答案
唯一理性的解释是 FormCreate
不执行。您需要将其分配给窗体的 OnCreate
事件处理程序。使用对象检查器这样做。
The only rational explanation is the FormCreate
is not executing. You need to assign it to the form's OnCreate
event handler. Use the object inspector to do so.
这篇关于为什么Application.OnException永远不会运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!