Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
245 views
in Technique[技术] by (71.8m points)

delphi - Why Application.OnException never runs?

Problem summary: The method assigned to Application.OnException never runs when an unhandled exception occurs.

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;

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.

I tried the same code (adapted) in Delphi 7 but the same result is encountered.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...